1. 程式人生 > >單元測試(三)

單元測試(三)

pan 如何 而且 rtt mage 圖片 父類 agent char

前言:前面講了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即可。

單元測試(三)