Spring集成Junit
Spring集成Junit的步骤:
1、导入spring集成Junit的坐标
org.springframework
spring-test
5.0.5.RELEASE
2、使用@Runwith注解替换原来的运行起期
+-+在加载完坐标后创建一个测试类,在类外使用@Runwith来代替晚来的运行过程@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:applicationContext.xml")//使用配置文件
@ContextConfiguration(classes = {SpringConfig.class})
public class SpringJunit {
@Autowired
private UserService userService;
@Test
public void test(){
userService.save();
}
}
3、使用@ContextConfiguration指定配置文件或配置类
+-+在在@Runwith下面使用@ContextConfiguration来指定需要加载的配置文件或配置类
4、使用@Autowire(@Resource)注入需要测试的对象
将会帮我们自动的注入对象
5、创建测试方法进行测试