mapper正常运行没问题,运行JUnit测试时,mapper就报错了
Spring boot + mybatis-plus项目,正常运行时没问题,运行JUnit测试时,mapper就报错了,不能用,报NullpointerException。看了网上很多都说是少了@RunWith(SpringJUnit4ClassRunner.class),但是我加上了还是一样但是我正常跑项目是没问题的,就是Junit会报错,就不知道为什么单元测试类:import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.howie.Application;
import com.howie.entity.TestEntity;
import com.howie.mapper.TestMapper;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SpringBootTestUnit {
@Autowired
TestMapper mapper;
@Test
public void test() {
String testId = "test1";
System.out.println("Junit test...");
LambdaQueryWrapper<TestEntity> wrapper = Wrappers.lambdaQuery();
wrapper.eq(TestEntity::testId, testId);
mapper.select(wrapper);
}
}
报错信息如下:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database.Cause: java.lang.NullPointerException
### The error may exist in com/howie/mapper/TestMapper.java (best guess)
### The error may involve com.howie.mapper.TestMapper.selectOne
### The error occurred while executing a query
### Cause: java.lang.NullPointerException
https://www.cnblogs.com/fanwenhao/p/10020252.html参考下这个 环境配置检查下 网上找下吧 可能junit的运行时的classpath信息与普通运行方式不同,于是将classpath修改为classpth*,使其从多个classpath中加载mapper文件 这个倒不清楚什么原因了,插个眼,看看大佬答案。
页:
[1]