Junit测试报错java.lang.NullPointerException空指针,以及mybatis-plus报空指针异常

首先应该是注解没有加上的问题,普通的测试类是无法获取bean的,所以才会报空指针。需要让这个测试类运行在spring测试环境中,添加以下注释:

@RunWith(SpringJUnit4ClassRunner.class)

我在测试mybatis-plus时,如果用了普通的juitTest,也是无法获取bean的,故也会报出空指针异常,我的解决办法是:
1.先导入spring-boot-staarter-test的依赖
2.然后
在类上用@SpringBootTest
方法上用 @Test(注意导入的是"import org.junit.jupiter.api.Test"这个包;)


```xml
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

```java
import org.junit.jupiter.api.Test 用在Spring Boot 2.2.X以后
import org.junit.Test用在2.2.x之前

@

猜你喜欢

转载自blog.csdn.net/song854601134/article/details/112527008