16 Implementing integration tests for Spring apps
This chapter covers
- Implementing Spring integration tests
- Implementing controller tests
A unit test checks a small piece of code in isolation. An integration test checks how multiple pieces work together. Instead of asking, “Does this method work by itself?”, an integration test asks, “Does this part of the application work correctly when it collaborates with the other parts it depends on?”
In a Spring application, an integration test usually verifies how your application logic works together with the capabilities provided by the Spring framework. Spring does a lot for your app: it creates objects, injects dependencies, reads configuration, starts web endpoints, manages transactions, connects to databases, applies security rules, and much more. A Spring integration test helps you check that your code behaves correctly when these framework features are actually involved.
In real-world apps, application logic rarely runs alone. A service may depend on a repository, a controller on request mapping and JSON conversion, a database operation on transactions, and a secured endpoint on authentication and authorization. Even if each class works correctly in isolation, the app can still fail when these parts do not work well together.