Spring 为什么要整合 Junit

image.png
image.png

Spring 整合 Junit 代码实现

1. 导入 Spring 集成 Junit 坐标

  1. <dependency>
  2. <groupId>org.springframework</groupId>
  3. <artifactId>spring-test</artifactId>
  4. <version>5.0.5.RELEASE</version>
  5. </dependency>

2. 使用 @Runwith 注解替换原来的运行期

image.png

3. 使用 @ContextConfiguration 指定配置文件或配置类

因为我们以后使用全注解开发,这里就演示指定配置类。
image.png

4. 使用 @Autowired 注入需要测试的对象

image.png

5. 创建测试方法进行测试即可

image.png