本文介绍了事务回滚在JUnit5的@Nested类中的测试用例中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用spring-boot,JUnit5,Mybatis。

I use 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)正在进行回滚。并输出以下日志。

(1) is working rollback. And the following log is output.

2017-05-26 22:21:29 [INFO ](TransactionContext.java:136) Rolled back transaction for test context ...

但是,(2)无效。我希望能够回滚到 @Nested

But, (2) is not working. I want to be able to roll back into @Nested.

推荐答案

这是预期的: Spring TestContext Framework 从未支持嵌套测试类的继承。

This is to be expected: the Spring TestContext Framework has never supported "inheritance" for nested test classes.

因此你的解决方法是实际上是在这个时间点实现目标的正确方法。

Thus your "work around" is actually the correct way to achieve your goal at this point in time.

但是,请注意,我可以为嵌套测试类与一起使用。

Note, however, that I may add support for "pseudo-inheritance" for nested test classes in conjunction with SPR-15366.

问候,

Sam( Spring TestContext Framework的作者

这篇关于事务回滚在JUnit5的@Nested类中的测试用例中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 14:05