Java Mybaitis plus遇到java.lang.NullPointerException

发生情景

在测试类测试mybaits时候,报了空指针异常,如下图

标题

image.png

对应Test类代码如下

package com.example.demo;

import com.example.demo.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


@SpringBootTest
public class ServiceTest {

    @Autowired
    private UserService userService;

    @Test
    public void testGetCount(){
        long count = userService.count();
        System.out.println(count);
    }

}

解决方案:

1. maven再编译一次

看到有人建议再编译一次,因为可能刚写的编译器还没加载到,试试去

image.png

还是失败

2. 在测试类添加@RunWith(SpringRunner.class)

这回成功了~

image.png