The @Rollback annotation tells the Spring TestContext Framework whether to roll back the transaction of a test method (or every test in a class) after it finishes. Test-managed transactions are rolled back by default, so @Rollback is mostly used as @Rollback(false) to commit instead, or as an explicit @Rollback(true) for readability. Its shortcut is @Commit, which is the same as @Rollback(false).
It only has an effect when the test runs inside a test-managed transaction, which you get from @Transactional on the test class or method. Without a transaction there is nothing to roll back.
org.springframework.test.annotation.Rollback is read by TransactionalTestExecutionListener, which is registered by default in the TestContext Framework. Before each @Transactional test method the listener starts a transaction through the PlatformTransactionManager. After the method it either rolls back or commits, depending on the @Rollback value.
The rules are simple:
@Transactional test with no @Rollback rolls back.@Rollback (value true) rolls back, which is the same as the default.@Rollback(false) or @Commit commits.@Transactional means no test-managed transaction, so the annotation has no effect.Rolling back keeps tests isolated. Each test starts from the same database state and you do not write cleanup code.
Older examples use @RunWith(SpringRunner.class), which is JUnit 4. With JUnit 5 (the default in Spring Boot 2.4 and later) @SpringBootTest already registers the Spring extension, so you do not add anything else.
The test sees its own changes because the service call and the assertions share the same transaction. When the method ends, Spring rolls the transaction back and the next test sees the original data.
Two details are worth knowing:
BigDecimal for money. The original double-based examples compare floating-point values, which is fragile.@DataJpaTest and @JdbcTest are already @Transactional, so they roll back by default too.Use @Rollback(false) when a test must commit, for example to check what another connection sees, or to leave data you want to inspect.
Committed data stays in the database. Later tests then depend on test order and on leftover rows, which is the flakiness rollback exists to prevent. If you must commit, clean up in an @AfterEach, use @Sql scripts with executionPhase = AFTER_TEST_METHOD, or use a throwaway database.
You can put @Rollback on the class to set the default for every test, then override it on individual methods.
Use a class-level @Rollback(false) sparingly. A whole class that commits is a sign that the tests should use a dedicated database or container.
For finer control, Spring provides hooks that complement @Rollback:
| Tool | Purpose |
|---|---|
@BeforeTransaction / @AfterTransaction | Run code outside the test transaction, before it starts or after it ends. |
TestTransaction.flagForCommit() | Switch the current test transaction to commit at runtime. |
TestTransaction.flagForRollback() | Switch it back to rollback. |
TestTransaction.end() and TestTransaction.start() | End the current transaction and begin a new one inside the same test. |
@SpringBootTest(webEnvironment = RANDOM_PORT) and a real HTTP client, the server handles the request on its own thread and commits its own transaction. The test transaction does not wrap it, so @Rollback cannot undo the data. Use MockMvc, which runs in the test thread, or clean up explicitly.REQUIRES_NEW escapes the test transaction. A service method with Propagation.REQUIRES_NEW commits independently.entityManager.flush() (or saveAndFlush) when you want to test database constraints.@Transactional but production code is not. A passing test can hide a LazyInitializationException that appears at runtime, because the open test transaction keeps the session alive.No. Test-managed transactions roll back by default, so @Rollback is only needed to commit (@Rollback(false)) or to override a class-level setting.
Nothing functional. @Commit is a composed annotation that is meta-annotated with @Rollback(false), and it reads better.
No. Without @Transactional (or a programmatic test transaction) there is no test-managed transaction, so the annotation does nothing.
Usually because the code under test ran in another thread or in a REQUIRES_NEW transaction, or because the test used @Commit or @Rollback(false).
@Rollback controls whether a Spring test-managed transaction ends in a rollback or a commit. Rollback is the default, which keeps tests isolated and removes cleanup code. Use @Rollback(false) or @Commit only when a test truly needs committed data, and remember that rollback does not reach other threads or REQUIRES_NEW transactions.