Here you want to guarantee that the code interacting with the external storage
saves and retrieves the information in the way you expect.
This type of test requires the service to connect to an external storage, when
testing you should use the same technology as the one used in the service’s
runtime.
The service example has a dedicated class to abstract the interaction with the
database from the rest of the application. Here that will be the
CoffeeBagsRepository.
The first iteration will focus on:
Representing the schema in code (Entity)
Persisting and retrieving the information (Repository)
To test this part you need to have a functional database and the ability to
easily perform migrations at any time.
Besides being functional, it needs to be fully automated, so you can create the
tests in your “local environment”, rely on the same set up to run the tests in
the delivery pipeline every time a change is pushed.
To do that in Spring Boot with Flyway, you will need the following:
// File: app/src/test/java/io/gh/boundaries/coffeestore/bag/persistence/CoffeeBagsRepositoryTest.java
@DataJpaTest@AutoConfigureTestDatabase(replace=AutoConfigureTestDatabase.Replace.NONE)classCoffeeBagsRepositoryTest{@AutowiredCoffeeBagsRepositoryrepository;@TestvoidretrievesAllCoffeeBags()throwsException{CoffeeBagEntitycoffeeBagEntity=newCoffeeBagEntity(UUID.randomUUID(),"Iridescent","Includes coffees from Ethiopia, Kenya, and Latin America. It’s a combination of some of our best, most interesting coffees, and features notes of dark chocolate and berry. Each year, we donate $1 per pound from Iridescent to fund transformative projects in coffee-producing countries through our Seeds fund—a program that awards grants to producer-driven sustainability projects. Because good work, good cheer, and great coffee is more than just a winter theme.",RoastingProfile.Dark,LocalDate.now().minusDays(2),"https://counterculturecoffee.com","GRM","g",newBigDecimal("340"));repository.saveAll(List.of(coffeeBagEntity));assertThat(repository.findAll()).containsOnly(coffeeBagEntity);}}