MybatisPlus使用出现错误java.lang.NullPointerException
使用MybatisPlus自动生成的代码,在Springboot项目中测试添加失败
发现自己在Controller层映射地址访问方法到时可以添加成功
但是唯独测试模块不行…
寻找原因:两种方式唯一不同的是,在映射过程中启动了项目…
解决:
1.在pom中添加依赖,**
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.在测试类上添加注解
@RunWith(SpringRunner.class)
@SpringBootTest(classes = EduApplication.class)
此处class为springboot启动类
@RunWith(SpringRunner.class)
通过启动项目来测试
@SpringBootTest(classes = EduApplication.class)
@Autowired
EduTeacherService eduTeacherService;
@Test
public void addTeacher(){
System.out.println(eduTeacherService);
EduTeacher teacher=new EduTeacher();
teacher.setIntro("教师简介");
teacher.setName("教师");
teacher.setLevel(1);
teacher.setSort(1);
teacher.setCareer("121");
eduTeacherService.save(teacher);
}
最终测试成功