mybatis-plus测试时发生Cannot invoke“xxx“because“this.userMapper“is null
mybatis-plus使用时发生 Cannot invoke "xxx"because "this.userMapper" is null,Cannot invoke "com.tsin.usercenter.mapper.UserMapper.selectList(com.baomidou.mybatisplus.core.conditions.Wrapper)"
·
目录
问题
在使用mybatis-plus时发生以上错误,原因是在springboot中,启动和测试默认在Application和Application中,如果不在就会发生mapper空值问题
解决方法一:
在applicationTest中进行测试,不要在自己创建的测试类中进行测试。注入使用@Resourse注解(在@Autowired注解不可行的时候)
解决方法二:
在自己的测试类中也是可以的,但要在类名前加注释@RunWith(SpringRunner.class),让spring boot可以访问指定的类,而且也建议使用@Resourse注解(在@Autowired注解不可行的时候)
@SpringBootTest
@RunWith(SpringRunner.class)//这行必须加
public class SampleTest {
@Resource
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
Assert.isTrue(5 == userList.size(), "");
userList.forEach(System.out::println);
}
}
更多推荐
已为社区贡献2条内容
所有评论(0)