In Spring Boot tests, @Rollback states whether the database changes made by a test are undone when the test ends. Spring rolls back a test-managed transaction by default, so tests stay isolated and repeatable, and @Rollback(false) is the way to keep the changes.
The annotation does nothing on its own. It works only when the test runs in a transaction, which happens with @Transactional or with test slices such as @DataJpaTest and @JdbcTest that are transactional by default.
Integration tests that write to a database can interfere with each other: one test inserts a user with a unique email, and the next test fails on the same email. Rolling back after every test removes that coupling without cleanup scripts, and it is fast because nothing is physically deleted.
Spring Boot gives you this through the Spring TestContext Framework. Before each test method it starts a transaction, and after the method it rolls the transaction back unless told otherwise.
@DataJpaTest configures only the JPA layer, uses an embedded database by default and wraps each test in a transaction. You do not need to add @Rollback for the default behaviour.
Whichever test runs first, the other still sees an empty table, because each test's transaction is rolled back.
With a full @SpringBootTest, add @Transactional yourself. Then @Rollback can state the intent or change it.
You can also put @Rollback on the class to set the default for all methods, and override it per method. Note that the old @RunWith(SpringRunner.class) belongs to JUnit 4; with JUnit 5, which Spring Boot uses today, @SpringBootTest already registers the Spring extension.
| Approach | Use when | Trade-off |
|---|---|---|
| Default rollback | Repository and service tests | Fast, isolated; cannot observe real commit behaviour |
@Commit or @Rollback(false) | Seeding shared data, testing commit hooks | Leaves data behind; needs cleanup |
No @Transactional, manual cleanup | Tests with real HTTP calls or multiple threads | Slower; you manage state |
| Container per run (Testcontainers) | Realistic database behaviour | Start-up cost, but removes cleanup worries |
REQUIRED propagation join the test transaction and are rolled back. Methods using REQUIRES_NEW commit in a separate transaction and are not rolled back.@SpringBootTest(webEnvironment = RANDOM_PORT), calls made with TestRestTemplate or WebTestClient over HTTP run on server threads, outside the test transaction, so their data is committed. With MockMvc the request runs on the test thread and does join the transaction.entityManager.flush() before asserting.No. Rollback is the default in a transactional test, so annotate only the methods that need to commit.
Usually the test is not transactional, the work ran in a REQUIRES_NEW transaction, or the request was handled on another thread by a real HTTP server. Check these first.
Yes. It is transactional by default, so each test rolls back its changes.
You cannot undo a commit. Clean the data in an @AfterEach method or use a fresh database per run.
@Rollback controls whether a Spring Boot test transaction is rolled back after the test. Rollback is the default for transactional tests and slices like @DataJpaTest, giving isolated, repeatable tests. Use @Rollback(false) or @Commit for deliberate commits, and remember that REQUIRES_NEW, real HTTP calls and non-database side effects fall outside the rollback.