一。应用场景:普通java web集成spring test,
项目结构:使用的是加入jar的方式,不是maven加入依赖的方式
注意:如果是引入jar包的方式,例如:spring-test4.0.5.jar 和junit4.12.jar的方式,
如果是使用的是junit4.12.jar的版本,要加入 hamcrest-core1.3.jar包,否则报如下错误:
java.lang.Exception: No tests found matching [{ExactMatcher:fDisplayName=test], {ExactMatcher:fDisplayName=test(org.devin.hellochat.test.Test)], {LeadingIdentifierMatcher:fClassName=org.devin.hellochat.test.Test,fLeadingIdentifier=test]] from org.junit.internal.requests.ClassRequest@31befd9f at org.junit.internal.requests.FilterRequest.getRunner(FilterRequest.java:40) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createFilteredTest(JUnit4TestLoader.java:77) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:68) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
二。使用:
1.applicationContext.xml spring-mvc.xml配置文件放在resource源文件下,注意:web.xml文件需要配置<Context-param></Context-param>,并引入applicationContext.xml文件,
sevlet配置中需要初始化spring-mvc.xml文件
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml","classpath:spring_mvc.xml"})
public class Test{
@Automired
private ItemCatService itemCatService;
@Test
public void test(){
System.out.println(itemCatService)
}
}
2.applicationContext.xml spring-mvc.xml配置文件放在WEB-INF目录下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"file:WebContent/WEB-INF/applicationContext.xml","file:WebContent/WEB-INF/spring_mvc.xml"})
public class Test{
@Automired
private ItemCatService itemCatService;
@Test
public void test(){
System.out.println(itemCatService)
}
}