20 Testing Java Persistence applications


This chapter covers

  • Introducing the testing pyramid and examining persistence testing in its context
  • Creating the persistence application to test using Spring Boot
  • Using the Spring TestContext Framework
  • Working with Spring profiles to test Java persistence applications
  • Demonstrating execution listeners to test Java persistence applications

All code needs to be tested. During development, we code, compile and run. When we run, we may sometimes test how our code works. Persistence applications mean more than this. They have the particularity that our code interacts with the external database and how our program works may be dependent on it.

20.1 Introducing the test pyramid

In previous chapters, we focused on developing the code to interact with the database. We examined different alternatives to do this, and different frameworks and we interacted with various databases. We need to make sure that our programs are safe and bug-free. We need to be able to introduce changes without creating bugs, add new features without impacting the old features, and refactor code without breaking existing functionality. This is the purpose of this last chapter.

20.2 Creating the persistence application to test

20.3 Using the Spring TestContext Framework

20.4 The @DirtiesContext annotation

20.5 @Transactional execution

20.6 The @BeforeTransaction and @AfterTransaction annotations

20.7 Working with Spring profiles

20.8 Working with test execution listeners

20.9 Summary