liveBook · Manning

Spring TestContext Framework

As explained in Java Persistence with Spring Data and Hibernate epub

The Spring TestContext Framework is a Spring testing framework that provides context management and integration-testing support independently of a particular testing framework.

Spring TestContext Framework provides testing-context management and integration-testing support independently of a particular testing framework. It fits persistence testing, where application behavior depends on an external database, and works with JUnit 5 and Spring Boot. section 20.0 section 20.3

Detecting and preventing dirty database state

A @SpringBootTest can inject UserRepository, save a User, retrieve all users, and assert the result. Repeating the test exposes state leakage: the first execution leaves its row behind, so the second finds two users. Transactional tests can isolate database changes; @Rollback is the default, while @Commit or @Rollback(false) preserves changes. section 20.3 section 20.9

Core mechanisms support context setup, lifecycle control, and database-test configuration:

  • TestContextManager manages one TestContext and dispatches events to registered TestExecutionListener instances. section 20.3
  • Default listeners prepare test instances, support dependency injection, and run around JUnit 5 lifecycle methods and test execution. section 20.8
  • Custom listeners replace automatic defaults unless MERGE_WITH_DEFAULTS is selected; merging preserves dependency injection and the default listener set. section 20.8
  • @ActiveProfiles selects Spring configuration profiles, while @DirtiesContext requests context reinitialization after test changes. section 20.8
Listing 20.5 The SaveRetrieveUserTest class
Path: Ch20/1 spring testing/src/test/java/com/manning/javapersistence
 /testing/SaveRetrieveUserTest.java
 
@SpringBootTest
class SaveRetrieveUserTest {
 
    @Autowired
    private UserRepository userRepository;
 
    @Test
    void saveRetrieve() {
        userRepository.save(new User("User1"));
        List<User> users = userRepository.findAll();
 
        assertAll(
                () -> assertEquals(1, users.size()),
                () -> assertEquals("User1", users.get(0).getName())
        );
    }
 
}

Use the complete save-and-retrieve test as a starting point for persistence integration tests, then add transaction or cleanup controls when repeated execution exposes shared state. section 20.3

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains context management, listener ordering, transaction cleanup, and profile configuration, but stops short of exhaustive annotation coverage.
Examples
Uses a repeated save-and-retrieve test, SQL table recreation and inserts, and a custom listener that traces transactional lifecycle events.
What it assumes
Assumes a Spring Boot persistence application, Spring-managed repositories, database configuration, and basic JUnit 5 test structure.
What you can do after
Build integration tests with injected repositories, control database isolation, select profiles, and extend listener-based lifecycle diagnostics.
How it connects
Connects integration testing with Spring Boot, JUnit 5, Spring profiles, transactions, repositories, and test execution listeners.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.