Chapter 10. Integration testing
This chapter covers
- Maven configuration for Spring integration testing
- Writing transactional happy-path tests
- Creating exception and performance tests
Ideally, testing is a core activity of the practicing software developer, and it so happens that the dependency-injection approach to software design is especially useful in the testing arena. Dependency injection is particularly useful in unit testing, where you desire to test small units of code in isolation from other code. With dependency injection you can inject mock dependencies with prescribed behavior into code units, which allows you to isolate faults to the code under test.
A step up from unit testing is integration testing. The idea here is to test collaborations between units of code. An integration test can be fairly narrow, perhaps involving a single collaboration between a web controller and a service bean (with DAO dependencies mocked out). At the other extreme it can involve deploying the code under test to a servlet container (or other container), sending HTTP requests and parsing HTTP responses for the expected output.
This chapter presents Spring’s TestContext framework, which offers features useful for both unit and integration testing. Here we’ll focus on integration testing because the features in this area are especially powerful.