• Spring boot Junit单元测试回滚


    在单元测试的时候,希望测试用例不影响其他测试结果,需要在方法级别回滚,代码如下:

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = ThanospjApplication.class)
    @Transactional  
    public class ApplicationTests {
    
        @Autowired
        private UserMapper userMapper;
    
        @Test
        @Rollback
        public void findByName() throws Exception {
            userMapper.insert("AAA", 20);
            User u = userMapper.findByName("AAA");
            Assert.assertEquals(20, u.getAge().intValue());
        }
    }
    Transactional注解会让Spring会在@Before 和 @After之间增加事务,Context上下文中有事务管理器就够了。
    10.3.5 Transaction文档
    In the TestContext framework, transactions are managed by the TransactionalTestExecutionListener. Note that TransactionalTestExecutionListener is configured by default, even if you do not explicitly declare @TestExecutionListeners on your test class. To enable support for transactions, however, you must provide a PlatformTransactionManager bean in the application context loaded by @ContextConfiguration semantics. In addition, you must declare @Transactional either at the class or method level for your tests.
     
    欢迎关注Java流水账公众号
  • 相关阅读:
    文件操作
    字典的相关函数
    列表相关操作/列表的相关函数
    字符串相关操作/字符串相关函数
    局部变量 与 全局变量
    函数名的使用
    函数的返回值 return
    命名关键字
    收集参数
    默认形参 与 关键字实参的区别
  • 原文地址:https://www.cnblogs.com/guofu-angela/p/9323365.html
Copyright © 2020-2023  润新知