Spring集成Junit


一、集成步骤

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

1.导入坐标


  org.springframework
  spring-test
  5.0.2.RELEASE


  junit
  junit
  4.12
  test

实例

@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();
  }
}