1,测试类中
1.1没有添加 @SpringBootTest
@SpringBootTest
只有加上该注解,测试类才能从spring容器中提取bean
如果注解爆红
说明pom文件没有加上springboot对单元测试的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
1.2没有添加 @RunWith(SpringRunner.class)
@RunWith(SpringRunner.class)
代表:启动和创建spring的应用上下文。
2,springboot启动类
启动类上有没有加上 @MapperScan("com.jt.mapper")
其中 com.jt.mapper是我自己mapper接口的包名
@MapperScan("com.jt.mapper")
加上该注解,spring容器才可以为接口创建代理对象,因为接口是不能创建对象的,所以才是代理对象
3,如果还是空指针请检查pom文件junit依赖版本是否过低
换上高版本的依赖即可,高版本的,我的就是这个问题
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
更多推荐