Spring集成Junit步骤
①在maven项目的pom.xml文件里面引入 spring-test 坐标依赖
②使用@Runwith注解替换原来的运行期
@RunWirh(SpringJunit4ClassRunner.class)
③使用@ContextConfifiguration指定配置文件或配置类
@ContextConfiguration('spring的核心配置文件')
④使用@Autowired注入需要测试的对象
⑤创建测试方法进行测试
代码如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = {"classpath:applicationContext.xml"})
public class SpringJunitTest {
// @Autowired
// @Qualifier("userService")
@Resource(name ="userService" )
private UserService userService;
@Test
public void test01(){
userService.save();
}
}