Refresh the database to ensure test isolation
masterTo prevent data leakage between tests, use the refresh attribute in @AutoConfigureEmbeddedDatabase. This resets the database to its initial state (including reverting committed changes) using fast template database copying rather than standard transaction rollbacks.
Available refresh modes:
AFTER_CLASS: Refreshes the database after each test class completes.AFTER_EACH_TEST_METHOD: Refreshes the database after every individual test method.
Note: If no refresh mode is specified, all tests in the project will share the same database instance.
@ExtendWith(SpringExtension.class)
@AutoConfigureEmbeddedDatabase(refresh = AFTER_EACH_TEST_METHOD)
public class EmptyDatabaseIntegrationTest {
@Test
void testMethod1() {
// fresh database
}
@Test
void testMethod2() {
// fresh database
}
}