使用MyBatis-Plus测试报错:xxxMapper,空指针异常:NullPointerException

错误:

java.lang.NullPointerException
    at com.bcl.vacancy.demo.mybatisplus.mytest.MyTest.MyTest(MyTest.java:26)
    at java.util.ArrayList.forEach(ArrayList.java:1259)
    at java.util.ArrayList.forEach(ArrayList.java:1259) 

测试代码:

@SpringBootTest
public class MyTest {

    @Resource
    private UserMapper userMapper;

    @Test
    public void MyTest(){
        User user = new User(11,"三三",201,"女","1");
        int insert = userMapper.insert(user);

        System.out.println("insert = " + insert);
    }
}

解决方法:在测试类头上加入:@SpringBootTest注解,即可解决。

猜你喜欢

转载自blog.csdn.net/DDDM456/article/details/124339730