Spring教程第二篇:Spring-Test(单元测试)

【Spring】 专栏收录该内容
8 篇文章 0 订阅

在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({"classpath:applicationContext.xml"})  
public class ReadDaoImplTest {  
    @Resource ReadDao readDao;  

    @Test  
    public void getListTest(){  
        List<Client> clientList = readDao.getList("client.test", null);  

        for(Client c:clientList){  
            System.out.println(c.getVersionNum());  
        }  
    }  
}

二、手动加载spring的配置文件,并启动spring容器

public class ReadDaoImplTest {  

    public  static void main(String[] args){  
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  

        context.start();  

        ReadDao fqaService = (ReadDao) context.getBean("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 class available.  
    at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:160)  
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)  
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:154)  
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:145)  
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)  
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)  
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)  
    at java.lang.reflect.Constructor.newInstance(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.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)  
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)  
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)  
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)  
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)  
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)  
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)  
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)  
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)  
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)  
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)  
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)  
Caused by: java.lang.NoClassDefFoundError: org/springframework/core/annotation/AnnotatedElementUtils  
    at org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper(BootstrapUtils.java:127)  
    ... 21 more  
Caused by: java.lang.ClassNotFoundException: org.springframework.core.annotation.AnnotatedElementUtils  
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)  
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)  
    at java.security.AccessController.doPrivileged(Native Method)  
    at java.net.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.也许你还会遇到诸如spring.xml文件找不着的错误解决方法如下,错误紧接着贴出如下
@ContextConfiguration(locations={“file:WebRoot/WEB-INF/applicationContext.xml”}) //WEB-INF下读取方式
//@ContextConfiguration(locations={“classpath:test/applicationContext.xml”})//类路径下有个test包下

java.lang.IllegalStateException: Failed to load ApplicationContext  
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)  
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)  
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)  
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)  
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)  
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)  
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)  
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)  
    at org.springframework.test.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 org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)  
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)  
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)  
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)  
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)  
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)  
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)  
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)  
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)  
    at org.eclipse.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/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [test/test/applicationContext.xml] cannot be opened because it does not exist  
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)  
    at org.springframework.beans.factory.xml.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 org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:205)  
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:100)  
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)  
    at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228)  
    at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)  
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)  
    ... 24 more  
Caused by: java.io.FileNotFoundException: class path resource [test/test/applicationContext.xml] cannot be opened because it does not exist  
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)  
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)  
    ... 35 more  
  • 7
    点赞
  • 1
    评论
  • 12
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值