Spring教程第⼆篇:Spring-Test(单元测试)
在Spring的框架下,做单元测试的两种办法:
⼀、使⽤spring中对Junit框架的整合功能
除了junit4和spring的jar包,还需要spring-test.jar。引⼊如下依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.0.7.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
@ContextConfiguration需要配上spring的配置⽂件,这样就可以在测试类中使⽤注解简单的注⼊需要的bean了。简单⾼效,Java代码如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"l"})
public class ReadDaoImplTest {
@Resource ReadDao readDao;
@Test
public void getListTest(){
List<Client> clientList = List("st", null);
for(Client c:clientList){
System.out.VersionNum());
}
}
}
⼆、⼿动加载spring的配置⽂件,并启动spring容器
public class ReadDaoImplTest {
public static void main(String[] args){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("l");
context.start();
ReadDao fqaService = (ReadDao) Bean("readDao");
System.out.println(fqaService);
}
}
Spring-Test中遇到的常见问题:
1.下载的\spring-test-3.1.
2.RELEASE.jar要和⾃⼰下载的spring项⽬的版本号⼀致,否则会以下报错
java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper at st.solveTestContextBootstrapper(BootstrapUtils.java:160)
at st.context.TestContextManager.<init>(TestContextManager.java:105)
at st.context.ateTestContextManager(SpringJUnit4ClassRunner.java:154)
at st.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:145)
wInstance0(Native Method)
wInstance(NativeConstructorAccessorImpl.java:57)
wInstance(DelegatingConstructorAccessorImpl.java:45)
at wInstance(Constructor.java:526)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
at org.del.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.del.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.Runner(ClassRequest.java:26)
lipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
lipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.ja
va:25)
lipse.jdt.internal.junit4.ateTest(JUnit4TestLoader.java:48)
lipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
lipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
lipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
lipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
lipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NoClassDefFoundError: org/springframework/core/annotation/AnnotatedElementUtils
at st.solveTestContextBootstrapper(BootstrapUtils.java:127)
... 21 more
Caused by: java.lang.ClassNotFoundException: annotation.AnnotatedElementUtils
at java.URLClassLoader$1.run(URLClassLoader.java:366)
at java.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 22 more
2.也许你还会遇到诸如l⽂件不着的错误解决⽅法如下,错误紧接着贴出如下
@ContextConfiguration(locations={“file:WebRoot/l”}) //WEB-INF下读取⽅式
//@ContextConfiguration(locations={“classpath:l”})//类路径下有个test包下
java.lang.IllegalStateException: Failed to load ApplicationContext
at st.ApplicationContext(TestContext.java:157)
at st.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener    at st.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListene    at st.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at st.context.ateTest(SpringJUnit4ClassRunner.java:211)
at st.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit
4ClassRunner.java:288)
at org.junit.del.ReflectiveCallable.run(ReflectiveCallable.java:12)
at st.context.hodBlock(SpringJUnit4ClassRunner.java:290)
at st.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at st.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at st.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
reference groupat org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at st.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
lipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
lipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
lipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
lipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
lipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
lipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [test/test/appli    at org.springframework.l.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
at org.springframework.l.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at st.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:205)
at st.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:100)
at st.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
at st.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228)
at st.context.TestContext.loadApplicationContext(TestContext.java:124)
at st.ApplicationContext(TestContext.java:148)
... 24 more
Caused by: java.io.FileNotFoundException: class path resource [test/l] cannot be opened because it does not exist
at InputStream(ClassPathResource.java:157)
at org.springframework.l.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
... 35 more

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。