springboot使用测试类报空指针异常

springboot使用测试类报空指针异常

之前在用springboot做增删改查时想测试是否能顺利连接数据库并取值。

但只要一加上test注解并做测试,屡屡报空,百思不得其解。

最后终于找到原因,将这个浪费很多时间的不算知识点的知识点记录一下。

首先检查依赖有无问题


org.springframework.boot
spring-boot-starter-test

之后,检查语句,找过mapper接口和mapper.xml的原因,找过service接口的原因,甚至还找过数据库的。

浪费了很多时间之后向百度求饶。

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test {
@Resource
SelectService selectService;
@org.junit.Test
public void query(){
List query = selectService.queryById(1001);
query.forEach(city1 -> System.out.println(city1));
}
}

springboot使用 test,需要添加两个注解@RunWith和@SpringBootTest。而@RunWith需要引用SpringRunner类。

关于这几个注解的释义有很多,就不多说了,互联网上有很多。

posted @ 2021-07-19 09:48  局点6828  阅读(973)  评论(0)    收藏  举报