Bootstrap

SpringBoot——单元测试error: can not execute. because can not find cache of TableInfo for entity!

问题

  单元测试时,遇到以下报错:

com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: error: can not execute. because can not find cache of TableInfo for entity!

	at com.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:49)
	at com.baomidou.mybatisplus.core.toolkit.Assert.isTrue(Assert.java:38)
	at com.baomidou.mybatisplus.core.toolkit.Assert.notNull(Assert.java:72)
	at com.baomidou.mybatisplus.extension.service.impl.ServiceImpl.saveOrUpdateBatch(ServiceImpl.java:174)
	at com.baomidou.mybatisplus.extension.service.IService.saveOrUpdateBatch(IService.java:91)
	... ....

解决方案

在测试类中,添加mybatis的初始化构建

    /**
     * before设置
     */
    @Before
    public void setUp() {
        //构建mybatis
        TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""), UserEntity.class);
    }
;