The @Rollback annotation in Spring's test framework tells the TestContext Framework whether the transaction of a test should be rolled back or committed when the test finishes. By default a test-managed transaction is rolled back, so @Rollback is mostly used as @Rollback(false) to commit, or to state the default explicitly.
@Rollback only has an effect when the test actually runs inside a transaction, for example because the test class or method is annotated with @Transactional. It is part of spring-test and applies to JUnit 5 tests and, with the matching runner, JUnit 4.
When a test method is annotated with @Transactional (or the class is, or the test uses a slice such as @DataJpaTest that is transactional by default), Spring starts a transaction before the test, runs the test inside it and ends it afterwards. The TransactionalTestExecutionListener decides how it ends, and the default is rollback.
That default gives you isolation for free: rows inserted by one test never reach another test, and you do not need cleanup code.
| Situation | Result at test end |
|---|---|
@Transactional, no @Rollback | Rolled back |
@Transactional with @Rollback or @Rollback(true) | Rolled back |
@Transactional with @Rollback(false) or @Commit | Committed |
No @Transactional | No test-managed transaction; @Rollback has no effect |
You can place @Rollback on a single test method or on the whole class. A method-level value overrides the class-level one.
The first test leaves the database untouched. The second one commits, so later tests and later test classes see the row unless you clean it up. Use that only for deliberate cases such as seeding a shared fixture.
@Commit is a composed annotation that means exactly @Rollback(false). It reads better in test code, so many teams prefer it.
If you commit data in tests, make the database disposable (an in-memory database or a container started per run) so leftover rows do not make the suite order dependent.
Rollback covers work done on the test's transaction. It does not cover everything.
REQUIRED joins the test transaction, so its changes are rolled back.REQUIRES_NEW runs in its own transaction and commits independently, so the rollback does not undo it.@SpringBootTest(webEnvironment = RANDOM_PORT) or DEFINED_PORT, requests made through a real HTTP client are handled on a server thread, outside the test transaction, so their changes are committed even if the test is @Transactional.entityManager.flush() in the test when you want the SQL to run.Use @BeforeTransaction and @AfterTransaction for set-up and checks that must run outside the test transaction. For finer control inside a test, TestTransaction lets you flag, commit or start a transaction programmatically.
No. Rollback is the default for test-managed transactions. Add @Rollback only to make the intent explicit or to switch it off with @Rollback(false).
No. Without a test-managed transaction there is nothing to roll back, so the annotation is ignored. Each repository call then runs in its own transaction and commits.
None in behaviour. @Commit is a shortcut for @Rollback(false).
Yes. Use @ExtendWith(SpringExtension.class), which @SpringBootTest already includes. The old @RunWith(SpringRunner.class) applies only to JUnit 4.
@Rollback controls whether a Spring test transaction is rolled back or committed at the end of a test. Rollback is the default, so the annotation matters mainly as @Rollback(false) or @Commit. It requires a test-managed transaction, does not undo REQUIRES_NEW work, HTTP requests handled on server threads or external side effects, and is best combined with a disposable test database.