...
/Testing a Persistence Adapter with Integration Tests
Testing a Persistence Adapter with Integration Tests
Learn how to test a persistence adapter with integration testing.
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:
Press + to interact
@DataJpaTest@Import({AccountPersistenceAdapter.class, AccountMapper.class})class AccountPersistenceAdapterTest {@Autowiredprivate AccountPersistenceAdapter adapter;@Autowiredprivate ActivityRepository activityRepository;@Test@Sql("AccountPersistenceAdapterTest.sql")void loadsAccount() {Account account = adapter.loadAccount(new AccountId(1L),LocalDateTime.of(2018, 8, 10, 0, 0));assertThat(account.getActivityWindow().getActivities()).hasSize(2);assertThat(account.calculateBalance()).isEqualTo(Money.of(500));}@Testvoid updatesActivities() {Account account = defaultAccount().withBaselineBalance(Money.of(555L)).withActivityWindow(new ActivityWindow(defaultActivity().withId(null).withMoney(Money.of(1L)).build())).build();adapter.updateActivities(account);assertThat(activityRepository.count()).isEqualTo(1);ActivityJpaEntity savedActivity = activityRepository.findAll().get(0);assertThat(savedActivity.getAmount()).isEqualTo(1L);}}
The @DataJpaTest
annotation
With @DataJpaTest
, we’re telling Spring ...