一. 步骤:
1. 拷贝jar包:
1. JUnit-4.9.jar和spring-test-4.2.4.RELEASE.jar ;
2. 替换原来的main函数:
1. 在测试类上使用注解方式替换: @RunWith(SpringJUnit4ClassRunner.class)
2. 在java中,main函数是程序唯一的执行入口;
3. 在单元测试虽然不用使用main函数也可以执行,真实情况是main函数已经写在了JUnit单元测试中;
4. 在Spring中整合JUnit时,需要使用spring-test-4.2.4.RELEASE.jar包,此包会负责加载Spring的配置文件,获取单元测试时需要的对象,但需要指定配置文件路径(由第三步完成);
3. 使用注解指定spring配置文件的位置:
1. 在测试类上使用注解方式指定配置文件位置:@ContextConfiguration(locations={"classpath:applicationContext.xml"});
2. locations为一个数组,可以加载多个配置文件;
3. classpath的作用是在类路径下查找配置文件;
原文:http://www.cnblogs.com/jiaw/p/6288276.html