Testing a Persistence Adapter with Integration Tests
Explore how to effectively test persistence adapters with integration tests in Hexagonal Architecture. Understand why real database interactions are crucial, how to use Spring's @DataJpaTest, and the benefits of running tests against actual databases rather than mocks or in-memory systems.
We'll cover the following...
As we did for the web controller, it makes sense to cover persistence adapters with integration tests instead of unit tests since we not only want to verify the logic within the adapter, but also the mapping into the database.
Account persistence adapter test
We want to test the persistence adapter we built in the chapter “Implementing a Persistence Adapter”.
The adapter has two methods, one for loading an Account entity from the database and another to save new account activities to the database:
The @DataJpaTest annotation
With @DataJpaTest, we’re telling Spring ...