单元测试

Posted yuxiaole

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单元测试相关的知识,希望对你有一定的参考价值。

前言:前面讲了java的junit,这里讲一下spring+junit。转载请注明出处:https://www.cnblogs.com/yuxiaole/p/9419224.html

Spring Test介绍

  当应用使用了Spring时,编写单测时需要每次手动的初始化Spring上下文,这种方式不仅繁琐,而且不能复用Spring上下文,导致单测执行时间变长,为此,Spring提供了对单测的支持,也就是Spring Test模块

  Spring和JUnit的整合,提供了对应的Runner和Rule,我们平常使用的比较多的是Spring的Runner,即SpringJUnit4ClassRunner或者SpringRunner(Spring4.3),Spring的Runner会根据配置自动初始化Spring上下文,并在单测方法执行时对其进行依赖注入,避免手动的getBean操作。

  Spring Test提供@ContextConfiguration来让我们指定要初始Spring上下文的配置,支持Spring的各种配置方式,如XML、JavaConfig等等方式,@ContextConfiguration和@RunWith等注解都可以注解在基类上,所以一般提供一个基类来简化单测的编写。

  默认情况下,子类可以基础父类的@ContextConfiguration配置,同时可以追加自己的配置,当程序非常模块化时,可以通过指定特定的配置文件来减少初始化Bean的数量,以便提高单测的执行速度。 

如何使用Spring+junit

  主要目的是使用依赖注入。

  可以使用注解的方式(注解加在需要测试的类上):

    @RunWith(SpringJunit4ClassRunner.class) ---->为了让测试在Spring容器环境下执行

    @ContextConfiguration(locations = {"classpath:applicationContext.xml"} --->用来指明Spring的配置文件位置

    @ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring-mvc.xml"}) --->或者这个用来指明Spring的配置文件位置

  pom文件配置:  

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.3.14.RELEASE</version>
      <scope>test</scope>
    </dependency>

  单测代码:  

package com.yule.demo.service.impl;

import com.yule.demo.service.DemoService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/spring-mvc.xml"})
//@ContextConfiguration(locations={"classpath:applicationContext.xml"})  //路径在resources下面
public class DemoServiceImplTest {

    @Autowired
    private DemoService demoService;

    @Test
    public void testAssertTrue() {
        this.demoService.test(3);
    }
} 

遇到问题:

  执行单测,控制台报错:

D:Javajdk1.8.0_144injava -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:Program FilesJetBrainsIntelliJ IDEA 2017.1.5libidea_rt.jar=53674:D:Program FilesJetBrainsIntelliJ IDEA 2017.1.5in" -Dfile.encoding=UTF-8 -classpath "D:Program FilesJetBrainsIntelliJ IDEA 2017.1.5libidea_rt.jar;D:Program FilesJetBrainsIntelliJ IDEA 2017.1.5pluginsjunitlibjunit-rt.jar;D:Javajdk1.8.0_144jrelibcharsets.jar;D:Javajdk1.8.0_144jrelibdeploy.jar;D:Javajdk1.8.0_144jrelibextaccess-bridge-64.jar;D:Javajdk1.8.0_144jrelibextcldrdata.jar;D:Javajdk1.8.0_144jrelibextdnsns.jar;D:Javajdk1.8.0_144jrelibextjaccess.jar;D:Javajdk1.8.0_144jrelibextjfxrt.jar;D:Javajdk1.8.0_144jrelibextlocaledata.jar;D:Javajdk1.8.0_144jrelibext
ashorn.jar;D:Javajdk1.8.0_144jrelibextsunec.jar;D:Javajdk1.8.0_144jrelibextsunjce_provider.jar;D:Javajdk1.8.0_144jrelibextsunmscapi.jar;D:Javajdk1.8.0_144jrelibextsunpkcs11.jar;D:Javajdk1.8.0_144jrelibextzipfs.jar;D:Javajdk1.8.0_144jrelibjavaws.jar;D:Javajdk1.8.0_144jrelibjce.jar;D:Javajdk1.8.0_144jrelibjfr.jar;D:Javajdk1.8.0_144jrelibjfxswt.jar;D:Javajdk1.8.0_144jrelibjsse.jar;D:Javajdk1.8.0_144jrelibmanagement-agent.jar;D:Javajdk1.8.0_144jrelibplugin.jar;D:Javajdk1.8.0_144jrelib
esources.jar;D:Javajdk1.8.0_144jrelib
t.jar;F:IDEAworkspacesdemo	arget	est-classes;F:IDEAworkspacesdemo	argetclasses;F:mavenRepositoryjunitjunit4.11junit-4.11.jar;F:mavenRepositoryorghamcresthamcrest-core1.3hamcrest-core-1.3.jar;F:mavenRepositoryorgspringframeworkspring-test4.3.14.RELEASEspring-test-4.3.14.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-context4.3.14.RELEASEspring-context-4.3.14.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-aop4.3.14.RELEASEspring-aop-4.3.14.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-expression4.3.14.RELEASEspring-expression-4.3.14.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-beans4.3.14.RELEASEspring-beans-4.3.14.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-core4.3.14.RELEASEspring-core-4.3.14.RELEASE.jar;F:mavenRepositorycommons-loggingcommons-logging1.2commons-logging-1.2.jar;F:mavenRepositoryorgspringframeworkspring-web4.3.14.RELEASEspring-web-4.3.14.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-webmvc4.3.14.RELEASEspring-webmvc-4.3.14.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-jdbc4.3.14.RELEASEspring-jdbc-4.3.14.RELEASE.jar;F:mavenRepositoryorgspringframeworkspring-tx4.3.14.RELEASEspring-tx-4.3.14.RELEASE.jar;F:mavenRepositoryjavaxservletjavax.servlet-api3.1.0javax.servlet-api-3.1.0.jar;F:mavenRepositoryjavaxservletjspjsp-api2.2jsp-api-2.2.jar;F:mavenRepositoryjavaxservletjstl1.2jstl-1.2.jar;F:mavenRepositorycommons-fileuploadcommons-fileupload1.2.1commons-fileupload-1.2.1.jar;F:mavenRepositorycommons-iocommons-io2.4commons-io-2.4.jar;F:mavenRepositoryorgslf4jslf4j-api1.7.25slf4j-api-1.7.25.jar;F:mavenRepositoryorgslf4jslf4j-log4j121.7.25slf4j-log4j12-1.7.25.jar;F:mavenRepositorylog4jlog4j1.2.17log4j-1.2.17.jar;F:mavenRepositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;F:mavenRepositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.yule.demo.service.impl.DemoServiceImplTest

java.lang.ExceptionInInitializerError
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    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 com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher.
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>(SpringJUnit4ClassRunner.java:102)
    ... 14 more

  找了半天才发现是版本问题,我从最后一行的报错点进去一看,代码里明确要求junit版本得是4.12或者更高。

技术分享图片

  所以解决办法就是讲 junit 版本调到4.12即可。 

 

以上是关于单元测试的主要内容,如果未能解决你的问题,请参考以下文章

四则运算单元测试

单元测试很棒,但是

词频统计单元测试

第1129期对vue.js单文件(.vue)进行单元测试

为啥我必须切换纹理单元才能让我的片段着色器识别要使用的纹理?

junit4单元测试--web项目中模拟登录会话,做全流程测试