码迷,mamicode.com
首页 > 其他好文 > 详细

单元测试(三)

时间:2018-08-04 19:07:01      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:如何   自己的   repos   log4   tar   ORC   deploy   目的   coding   

前言:前面讲了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:\Java\jdk1.8.0_144\bin\java -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.5\lib\idea_rt.jar=53674:D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.5\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.5\lib\idea_rt.jar;D:\Program Files\JetBrains\IntelliJ IDEA 2017.1.5\plugins\junit\lib\junit-rt.jar;D:\Java\jdk1.8.0_144\jre\lib\charsets.jar;D:\Java\jdk1.8.0_144\jre\lib\deploy.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_144\jre\lib\javaws.jar;D:\Java\jdk1.8.0_144\jre\lib\jce.jar;D:\Java\jdk1.8.0_144\jre\lib\jfr.jar;D:\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_144\jre\lib\jsse.jar;D:\Java\jdk1.8.0_144\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_144\jre\lib\plugin.jar;D:\Java\jdk1.8.0_144\jre\lib\resources.jar;D:\Java\jdk1.8.0_144\jre\lib\rt.jar;F:\IDEAworkspace\sdemo\target\test-classes;F:\IDEAworkspace\sdemo\target\classes;F:\mavenRepository\junit\junit\4.11\junit-4.11.jar;F:\mavenRepository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;F:\mavenRepository\org\springframework\spring-test\4.3.14.RELEASE\spring-test-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-context\4.3.14.RELEASE\spring-context-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-aop\4.3.14.RELEASE\spring-aop-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-expression\4.3.14.RELEASE\spring-expression-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-beans\4.3.14.RELEASE\spring-beans-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-core\4.3.14.RELEASE\spring-core-4.3.14.RELEASE.jar;F:\mavenRepository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;F:\mavenRepository\org\springframework\spring-web\4.3.14.RELEASE\spring-web-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-webmvc\4.3.14.RELEASE\spring-webmvc-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-jdbc\4.3.14.RELEASE\spring-jdbc-4.3.14.RELEASE.jar;F:\mavenRepository\org\springframework\spring-tx\4.3.14.RELEASE\spring-tx-4.3.14.RELEASE.jar;F:\mavenRepository\javax\servlet\javax.servlet-api\3.1.0\javax.servlet-api-3.1.0.jar;F:\mavenRepository\javax\servlet\jsp\jsp-api\2.2\jsp-api-2.2.jar;F:\mavenRepository\javax\servlet\jstl\1.2\jstl-1.2.jar;F:\mavenRepository\commons-fileupload\commons-fileupload\1.2.1\commons-fileupload-1.2.1.jar;F:\mavenRepository\commons-io\commons-io\2.4\commons-io-2.4.jar;F:\mavenRepository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;F:\mavenRepository\org\slf4j\slf4j-log4j12\1.7.25\slf4j-log4j12-1.7.25.jar;F:\mavenRepository\log4j\log4j\1.2.17\log4j-1.2.17.jar;F:\mavenRepository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;F:\mavenRepository\ch\qos\logback\logback-core\1.2.3\logback-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即可。 

 

单元测试(三)

标签:如何   自己的   repos   log4   tar   ORC   deploy   目的   coding   

原文地址:https://www.cnblogs.com/yuxiaole/p/9419224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!