当前使用版本(必填,否则不予处理)

3.5.3.1

该问题是如何引起的?(确定最新版也有问题再提!!!)

spring-test 使用@MockBean会触发spring上下文重建以及使用之前缓存的上下文的情况。而TableInfoHelper里的Configuration和使用的spring上下文不匹配,导致batch方法无法回滚。 之前有人提过类似的问题,当时的回复是新的Configuration会覆盖旧的,但是这里还存在复用旧上下文的情况。 https://github.com/baomidou/mybatis-plus/issues/732#issue-393944579 目前的解决办法是监听每个测试前的事件,手动设置Configuration,但是不确定有没有其他变量需要替换,或者有没有更好的办法

public class MybatisTestConfig {

    @Autowired
    SqlSessionFactory factory;

    /**
     * 监听每个测试方法前的事件
     * @param event 事件
     */
    @EventListener()
    public void setConfiguration(PrepareTestInstanceEvent event)  {
        TableInfoHelper.getTableInfos().forEach(tableInfo -> {
            try {
                Field field = tableInfo.getClass().getDeclaredField("configuration");
                field.setAccessible(true);
                field.set(tableInfo,factory.getConfiguration());
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }
        });
    }
}

重现步骤(如果有就写完整)

复现demo:https://github.com/wangwenwwx/demo 1.三个测试类一起跑,第三个会回滚失败,因为第一个和第三个复用了相同的spring上下文 MyBatis-Plus spring-boot执行测试时,batch方法事务回滚失败 2.单独跑第三个测试类,正常通过 MyBatis-Plus spring-boot执行测试时,batch方法事务回滚失败

@SpringBootTest
@Transactional
public class MyTableServiceTestA {

    @MockBean
    TestFeign feign;

    @Autowired
    MyTableService service;

    @Test
    void delete() {
        service.removeBatchByIds(Collections.singletonList(10001));
        Assertions.assertNull(service.getById(10001));
    }

    @Test
    void testIsExist() {
        Assertions.assertNotNull(service.getById(10001));
    }
}

报错信息

事务未回滚

Comment From: nieqiurong

把这个版本的jar包解压到本地仓库试试.

链接:https://pan.baidu.com/s/1DgnEW7TBStM8HRuQpd2f_w 提取码:1111

Comment From: wangwenwwx

把这个版本的jar包解压到本地仓库试试.

链接:https://pan.baidu.com/s/1DgnEW7TBStM8HRuQpd2f_w 提取码:1111

好了