也算是今天才知道:普通的测试类是无法获取bean的,所以才会报空指针。因此我们需要让这个测试类能获取到bean。
解决办法:
- 首先,确保已经在pom.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>
- 在测试类上加下面的注解
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
- 问题解决。不得不说,mybatisplus真香。