Spring集成Junit

一、集成步骤

① 导入spring集成Junit的坐标
② 使用@Runwith注解替换原来的运行期
③ 使用@ContextConfiguration指定配置文件或配置类
④ 使用@Autowired注入需要测试的对象
⑤ 创建测试方法进行测试

 

1.导入坐标

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>

  

实例

@RunWith(SpringJUnit4ClassRunner.class)

// 加载 spring 核心配置文件
//@ContextConfiguration(value = {"classpath:applicationContext.xml"})

@ContextConfiguration(classes = {SpringConfiguration.class})
public class SpringJunitTest {
  @Autowired
  private UserService userService;
  @Test
  public void testUserService(){
    userService.save();
  }
}

posted @ 2022-03-08 22:53  FinnYY  阅读(52)  评论(0编辑  收藏  举报