JUnit是一個(gè)Java語(yǔ)言的單元測(cè)試框架。它由Kent Beck和Erich Gamma建立,逐漸成為源于Kent Beck的sUnit的xUnit家族中最為成功的一個(gè)。 JUnit有它自己的JUnit擴(kuò)展生態(tài)圈。多數(shù)Java的開(kāi)發(fā)環(huán)境都已經(jīng)集成了JUnit作為單元測(cè)試的工具。
JUnit是由 Erich Gamma 和 Kent Beck 編寫(xiě)的一個(gè)回歸測(cè)試框架(regression testing framework)。Junit測(cè)試是程序員測(cè)試,即所謂白盒測(cè)試,因?yàn)槌绦騿T知道被測(cè)試的軟件如何(How)完成功能和完成什么樣(What)的功能。Junit是一套框架,繼承TestCase類(lèi),就可以用Junit進(jìn)行自動(dòng)測(cè)試了。
From 百度百科
1、從命令行運(yùn)行Junit單元測(cè)試
新建一個(gè)待測(cè)試的類(lèi),Compute.java:
/**
* Created by chengxia on 2019/3/28.
*/
public class Compute {
public static int add(int a, int b){
return a+b;
}
}
為上面的類(lèi)新建一個(gè)測(cè)試類(lèi):
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Created by chengxia on 2019/4/6.
*/
public class ComputeTest {
@Test
public void testCompute(){
int result = Compute.add(3,5);
assertEquals(8,result);
}
}
通過(guò)命令行進(jìn)入上面兩個(gè)源碼文件所在的目錄,然后編譯并運(yùn)行Junit測(cè)試。如下:
$ ls
Compute.java ComputeTest.java
$ javac -cp .:/Applications/IntelliJ\ IDEA.app/Contents/lib/junit-4.12.jar ComputeTest.java
$ ls
Compute.class Compute.java ComputeTest.class ComputeTest.java
$ java -cp .:/Applications/IntelliJ\ IDEA.app/Contents/lib/junit-4.12.jar:/Applications/IntelliJ\ IDEA.app/Contents/lib/hamcrest-core-1.3.jar org.junit.runner.JUnitCore ComputeTest
JUnit version 4.12
.
Time: 0.004
OK (1 test)
$
上面需要注意:
(1) 由于是引入的jar包路徑中有空格,所以,在命令行中需要通過(guò)反斜杠進(jìn)行轉(zhuǎn)義。
(2) 編譯時(shí),引入一個(gè)jar包(/Applications/IntelliJ\ IDEA.app/Contents/lib/junit-4.12.jar
)就可以,在運(yùn)行時(shí)必須同時(shí)引入/Applications/IntelliJ\ IDEA.app/Contents/lib/hamcrest-core-1.3.jar
。否則,會(huì)報(bào)錯(cuò)。如下:
$ java -cp .:/Applications/IntelliJ\ IDEA.app/Contents/lib/junit-4.12.jar org.junit.runner.JUnitCore ComputeTest
JUnit version 4.12
Exception in thread "main" java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at org.junit.runner.Request.classes(Request.java:75)
at org.junit.runner.JUnitCommandLineParseResult.createRequest(JUnitCommandLineParseResult.java:118)
at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77)
at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
Caused by: java.lang.ClassNotFoundException: org.hamcrest.SelfDescribing
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 17 more
$
很明顯,junit-4.12.jar
中用到了hamcrest-core-1.3.jar
中的依賴(lài)。
從上面運(yùn)行junit測(cè)試的命令java -cp .:/Applications/IntelliJ\ IDEA.app/Contents/lib/junit-4.12.jar:/Applications/IntelliJ\ IDEA.app/Contents/lib/hamcrest-core-1.3.jar org.junit.runner.JUnitCore ComputeTest
可以看出,這里直接運(yùn)行的實(shí)際是JUnitCore類(lèi),測(cè)試類(lèi)名只是作為一個(gè)參數(shù)傳遞給JUnitCore類(lèi)的。這里,我們可以推測(cè),JUnitCore類(lèi)中肯定有main方法。實(shí)際上,借助IDEA的自動(dòng)反編譯打開(kāi)這個(gè)類(lèi),其中確實(shí)有main方法定義。如下:
package org.junit.runner;
import junit.framework.Test;
import junit.runner.Version;
import org.junit.internal.JUnitSystem;
import org.junit.internal.RealSystem;
import org.junit.internal.TextListener;
import org.junit.internal.runners.JUnit38ClassRunner;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
public class JUnitCore {
... ...
public static void main(String... args) {
Result result = (new JUnitCore()).runMain(new RealSystem(), args);
System.exit(result.wasSuccessful()?0:1);
}
... ...
}
2、在IDE中創(chuàng)建并運(yùn)行單元測(cè)試(IntelliJ IDEA
為例)
上面的過(guò)程有助于我們理解單元測(cè)試的整個(gè)運(yùn)行機(jī)制,實(shí)際開(kāi)發(fā)中并沒(méi)有這么麻煩。一般,我們都通過(guò)使用IDE來(lái)創(chuàng)建單元測(cè)試。這里我們,以IDEA為例說(shuō)明(版本:IntelliJ IDEA 2017.1.5
,自帶有junit插件)。
假設(shè),工程中有如下Compute類(lèi):
/**
* Created by chengxia on 2019/3/28.
*/
public class Compute {
public static int add(int a, int b){
return a+b;
}
}
工程的結(jié)構(gòu)如下:
現(xiàn)在,我們要為這個(gè)類(lèi)創(chuàng)建單元測(cè)試。首先,新建一個(gè)單元測(cè)試的文件夾。在IDEA中這需要兩步:
(1) 新建一個(gè)目錄。
(2) 將上面新建的目錄設(shè)置為單元測(cè)試的根目錄。
完成之后,效果如下:
接下來(lái),創(chuàng)建單元測(cè)試的類(lèi)。因?yàn)橛胁寮@里直接選中工作區(qū)中要?jiǎng)?chuàng)建單元測(cè)試的類(lèi),然后直接按快捷鍵command+shift+T
,IDEA會(huì)自動(dòng)彈出創(chuàng)建單元測(cè)試的指引。如下:
點(diǎn)擊Create New Test...
,彈出如下對(duì)話(huà)框,選擇junit的版本,選擇要測(cè)試的方法,點(diǎn)OK即可。如下:
注意,上圖中提示junit4依賴(lài)的庫(kù)找不到。點(diǎn)擊Fix,然后,IDEA會(huì)提示,選擇junit4 jar包路徑,或者使用IDEA自帶的junit4 jar包。我們這里選擇IDEA自帶的jar包。
最后,IDEA自動(dòng)生成的測(cè)試類(lèi)(放在剛創(chuàng)建的test目錄中)如下。
我們要做的就是簡(jiǎn)單地將測(cè)試邏輯補(bǔ)全,如下:
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Created by chengxia on 2019/4/6.
*/
public class ComputeTest {
@Test
public void add() throws Exception {
assertEquals(5,Compute.add(3,2));
}
}
這樣,我們?cè)谏厦娴臏y(cè)試類(lèi)中,右鍵,然后選擇Run 'ComputeTest'
,即可運(yùn)行單元測(cè)試,效果如下。