我使用spring-boot,JUnit5,Mybatis。
@SpringJUnitJupiterConfig(classes = {RepositoryTestConfig.class}) @MapperScan @Rollback @Transactional public class TestClass { @Autowired private TestMapper testMapper; @BeforeEach void init() { User user = new User(); testMapper.insert(user); } @Test public void test1() { // (1) success rollback } @Nested class WhenExistData { @Test public void test2() { // (2) rollback not working } } }
(1)正在工作回滚。并输出以下日志。
2017-05-26 22:21:29 [INFO ](TransactionContext.java:136) Rolled back transaction for test context ...
但是,(2)不起作用。我希望能够回滚到@Nested。
@Nested
我用以下方式解决了。
@SpringJUnitJupiterConfig(classes = {RepositoryTestConfig.class}) @MapperScan @Rollback @Transactional public class TestClass { @Autowired private TestMapper testMapper; @BeforeEach void init() { User user = new User(); testMapper.insert(user); } @Nested @SpringJUnitJupiterConfig(classes = {RepositoryTestConfig.class}) @MapperScan @Rollback @Transactional class WhenExistData { @Test public void test2() { } } }