How it works...

It is ideal to run tests against all repositories and service methods before applying them to any request-response transactions. To test all these components, we need to load all the necessary context definitions to Spring TestContext in order to access every autowiring detail of each bean. To test the correctness and effectiveness of each method, we have to execute the actual methods or perform isolated testing using mocking.

Although quite risky, TestDepartmentDao executes the actual repository transactions that deal with the actual database schema. This can be effective, as long as the data involved is not live. On the other hand, TestDepartmentService prefers to use the @Mock objects to be injected into some @InjectMock ...

Get Spring 5.0 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.