Spring整合Junit4进行单元测试
需要添加spring-text.RELEASE.jar。 需要添加Junit4的2个jar包:junit.jar、hamcrest-core.jar。 写Junit的注解,Alt+Enter添加即可。 也可以自己下载添加: https://github.com/junit-team/junit4/wiki/Download-and-Install 示例 @RunWith(SpringJUnit4ClassRunner.class) //值是String数组,可以写多个xml配置文件 @ContextConfiguration(locations = {"classpath:spring-config.xml"}) public class Test { @Autowired private User user; @org.junit.Test public void test1(){ System.out.println(user); } @org.junit.Test public void test2(){ System.out.println(user); } @org.junit.Test public void test3(){ System.out.println(user); } } 在测试类上标注: @RunWith 会自动根据配置创建Spring容器,无需 new