造成空指针异常的原因是 自动装配没有生效 我们做的就是让自动装配生效
首先导入springboot的test依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
然后在测试类的上面添加注解 告诉sprringboot这是一个测试类
@RunWith(SpringJUnit4ClassRunner.class)
//这里是主启动类的反射对象 DeptProvider_8001将这个改成主启动类的名字即可
@SpringBootTest(classes = DeptProvider_8001.class)