Testing Code
Terminology
- Test Fixture - A fixed state of a set of objects used as a baseline for running tests. There should be a well known and fixed environment in which tests are run so that the results are repeatable.
- Unit Tests / Unit Testing - Code written to test code uder test
- Designed to test specific sections of code
- Ideally code coverage is maybe 70-80%
- Should be small and fast
- No external dependencies
- No databases or spring components
- Integration Tests - Designed to test behaviors between objecrts and parts of the overall system
- Much larger scope
- Can include the Spring Context, databases, and message brokers
- Much slower than unit tests
- Functional Tests - Testing a running application
- Likely deployed in a test env
- Functional touch points are tested
- Test Driven Development - Write Tests first, which of course fail, then code to 'fix' it
- BDD - Specifies that tests of any unit of software should be specified in terms of desired behavior of the unit
- Often implemented with DSLs to create natural language tests
- JBehave, Cucumber, Spock
- Mock - A fake implementation of a class used for testing. Like a test Double.
- Spy - A partial mock, allowing you to override select methods of a real class.
- Generally Mocking is more popular than Spying
Testing Goals
- Generally you want the majority of tests to be unit tests.
- Bringing up the Spring Context makes your test exponentially slower
- Try to test specific business logic in unit tests
- Use integration tests to test interactions
- Think of a pyramid. Base is unit tests, middle is integration, top is functional tests
Test Scope Dependencies
- Using spring-boot-starter-test will load the following:
- JUnit - the de-facto standard for unit testing
- Spring Test and Spring Boot Test - Utilites and integration test support for Spring Boot applications
- AssertJ - fluent assertion library
- Hamcrest - A library of matcher objects
- Mockito - A Java mocking framework
- JSONassert - An assertion library for JSON
- JSONPath - XPath for JSON
No Comments