@Rollback

@Rollback 指示在测试方法完成后是否应该回滚事务测试方法的事务。如果是 true,则回滚事务。否则,则提交事务(另请参见@Commit )。即使没有明确声明 @Rollback ,SpringTestContext 框架中集成测试的回滚也默认为 true

@Rollback indicates whether the transaction for a transactional test method should be rolled back after the test method has completed. If true, the transaction is rolled back. Otherwise, the transaction is committed (see also @Commit). Rollback for integration tests in the Spring TestContext Framework defaults to true even if @Rollback is not explicitly declared.

当声明为类级注解时,@Rollback 为测试类层次结构中的所有测试方法定义默认回滚语义。当声明为方法级注解时,@Rollback 为特定测试方法定义回滚语义,可能覆盖类级 @Rollback@Commit 语义。

When declared as a class-level annotation, @Rollback defines the default rollback semantics for all test methods within the test class hierarchy. When declared as a method-level annotation, @Rollback defines rollback semantics for the specific test method, potentially overriding class-level @Rollback or @Commit semantics.

以下示例导致不回滚测试方法的结果(即,提交结果到数据库):

The following example causes a test method’s result to not be rolled back (that is, the result is committed to the database):

Java
@Rollback(false) (1)
@Test
void testProcessWithoutRollback() {
	// ...
}
1 Do not roll back the result.
Kotlin
@Rollback(false) (1)
@Test
fun testProcessWithoutRollback() {
	// ...
}
2 Do not roll back the result.