注意事項:
- 測試方法上必須使用@Test修飾
- 測試方法必須使用public void進行修飾脖隶,不能帶任何參數(shù)
- 新建一個源代碼目錄存放測試代碼
- 測試類的包應(yīng)該和被測試類的包保持一致
- 測試單元中的每個方法必須可以獨立測試粤咪,互相之間不能有依賴關(guān)系
- 測試類使用(類型+Test)作為測試類名(不是必須七嫌,但最好養(yǎng)成這個習(xí)慣)
- 測試方法使用test作為方法名的前綴(不是必須夯秃,但最好養(yǎng)成這個習(xí)慣)
IDE version:IntelliJ IDEA 15.0.6
JUnit version:4.10
一土榴、JUnit快速入門
-
項目結(jié)構(gòu):
Paste_Image.png - Calculate.java
package com.amber.junittest;
/**
* Created by amber on 2017/6/13.
*/
public class Calculate {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public int multiply(int a, int b) {
return a * b;
}
public int divide(int a, int b) {
return a / b;
}
}
-
在Calculate類頁面厦画,使用右鍵或者快捷鍵Shift+Alt+T嘱能,彈出下圖:
Paste_Image.png
Paste_Image.png
Paste_Image.png
就會生成CalculateTest.java測試類:
package com.amber.junittest;
import org.junit.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Created by amber on 2017/6/13.
*/
public class CalculateTest {
@Test
public void testAdd() throws Exception {
Assert.assertEquals(5, new Calculate().add(2, 3));
}
@Test
public void testSubtract() throws Exception {
Assert.assertEquals(5, new Calculate().subtract(2, 3));
}
@Test
public void testMultiply() throws Exception {
Assert.assertEquals(5, new Calculate().multiply(2, 3));
}
@Test
public void testDivide() throws Exception {
Assert.assertEquals(5, new Calculate().divide(2, 3));
}
}
- 測試結(jié)果說明:
- Failure一般由單元測試使用的斷言(Assert.assertEquals())方法判斷失敗所引起的,表示測試點發(fā)現(xiàn)了問題伦吠,就是說名程序的輸出結(jié)果和我們預(yù)期的不一樣妆兑。
- error是由于代碼異常引起的魂拦,它可以產(chǎn)生于測試代碼本身的錯誤,也可以是被測試代碼中的一個隱藏的bug箭跳。
- 測試用例不是用來證明你是對的晨另,而是用來證明你沒有錯!
如下圖所示:橙色感嘆號代表是Failure谱姓、紅色代表是error、綠色代表success刨晴。
全部正確屉来,都是綠色的,看著真舒服~
二狈癞、JUnit運行流程
-
再生成一個測試類茄靠,這回多選兩個選項,會在測試類中生成一個帶注解@Before的方法和@After的方法蝶桶。
Paste_Image.png - 在測試類中添加注解@BeforeClass和 @AfterClass的方法:
package com.amber.junittest;
import org.junit.*;
import static org.junit.Assert.*;
/**
* Created by amber on 2017/6/13.
*/
public class CalculateTest2 {
@BeforeClass
public static void setUpBeforeClass() {
System.out.println("this is BeforeClass...");
}
@Before
public void setUp() throws Exception {
System.out.println("\nthis is Before...");
}
@After
public void tearDown() throws Exception {
System.out.println("this is After...");
}
@AfterClass
public static void setTearDownAfterClass() {
System.out.println("this is AfterClass...");
}
@Test
public void test1(){
System.out.println("this is Test1...");
}
@Test
public void test2(){
System.out.println("this is Test2...");
}
}
- 執(zhí)行測試后慨绳,結(jié)果為:
this is BeforeClass...
this is Before...
this is Test1...
this is After...
this is Before...
this is Test2...
this is After...
this is AfterClass...
仔細(xì)觀察結(jié)果,我們發(fā)現(xiàn):
1. @BeforeClass修飾的方法會在其他方法被調(diào)用前執(zhí)行真竖,而且該方法是靜態(tài)的脐雪,所以當(dāng)測試類被加載后接著就會運行它,且在內(nèi)存中它只會有一份實例恢共,所以它比較適合加載配置文件战秋。
2. @AfterClass修飾的方法會在其他方法被調(diào)用后執(zhí)行,通常會用來對資源的清理讨韭,如關(guān)閉數(shù)據(jù)庫的連接脂信。
3. @Bfore和@After所修飾的方法會在每個測試方法的前后各執(zhí)行一次。
三透硝、JUnit常用注解
我們大致的五個注解已經(jīng)有了解了狰闪,下面再深入了解@Test測試注解還有什么小技能。
- @Test(expected = xxx.class)這是可以捕獲預(yù)期會遇到的異常的一個待參數(shù)注解濒生,只要正確的預(yù)期到會出現(xiàn)的異常埋泵,那么測試運行就不會報錯了。
@Test(expected = ArithmeticException.class)
public void testDivide() throws Exception {
Assert.assertEquals(0, new Calculate().divide(3, 0));
}
因為0不能做除數(shù)甜攀,所以肯定會出現(xiàn)一個ArithmeticException秋泄,我們通過@Test(expected = ArithmeticException.class)正確的捕獲了異常,運行結(jié)果當(dāng)然是success规阀!
- @Test(timeout = 毫秒)恒序,這個參數(shù)見名知其意,如果方法執(zhí)行時間小于超時限定時間谁撼,則success歧胁。反之滋饲,則會強行停止方法運行,并報出failure喊巍。
看第一個例子屠缭,設(shè)置個死循環(huán),設(shè)置超時100毫秒崭参。
@Test(timeout = 100)
public void testWhile() {
while (true) {
System.out.println("run forever");
}
}
結(jié)果如圖呵曹,因為是死循環(huán),所以肯定會超時何暮,100毫秒后奄喂,方法被強行停止。
看第二個例子海洼,讓當(dāng)前線程沉睡100毫秒跨新,超時時間是1秒。
@Test(timeout = 1000)
public void testTimeout() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
結(jié)果:
- @Ignore注解所修飾的方法會被測試運行器忽略坏逢。(太簡單了域帐,不做測試了)
- @RunWith注解可以更改測試運行器。只要類繼承org.junit.runner.Runner是整。(這個后面會詳解)
- junit斷言參考幫助文檔:http://junit.org/junit4/javadoc/latest/
四肖揣、JUnit測試套件的使用
測試套件就是可以一次性批量執(zhí)行測試的一個類。下面開始編寫:
- 在test包下新建SuiteTest類贰盗,并使用@RunWith(Suite.class)注解更改測試運行器為Suite.class许饿,使用@Suite.SuiteClasses(數(shù)組)將測試類添加到數(shù)組:
package com.amber.junittest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* Created by amber on 2017/6/13.
*/
@RunWith(Suite.class)
@Suite.SuiteClasses(value = {TaskTest1.class,TaskTest2.class,TaskTest3.class,TaskTest4.class})
public class SuiteTest {
}
運行:
總結(jié):
- 測試套件就是組織測試類一起運行的。
- 新建一個測試套件的入口類舵盈,這個類中不能包含其他的方法陋率。
- 使用@RunWith(Suite.class)更改測試運行器為Suite.class。
- 將要測試的類作為數(shù)組傳到@Suite.SuiteClasses({})秽晚。
五瓦糟、JUnit的參數(shù)化設(shè)置
- 更改默認(rèn)的測試運行器為@RunWith(Parameterized.class)
- 聲明變量來存放預(yù)期值和結(jié)果值(參數(shù)值)
- 聲明一個返回值為Collection的公共靜態(tài)方法句伶,并使用@Parameterized.Parameters進行修飾
- 為測試類聲明一個帶有參數(shù)的公共構(gòu)造函數(shù)劲蜻,并在其中為之聲明變量賦值
package com.amber.junittest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
/**
* Created by amber on 2017/6/13.
*/
@RunWith(Parameterized.class)
public class ParameterTest {
int expected = 0;
int input1 = 0;
int input2 = 0;
@Parameterized.Parameters
public static Collection<Object[]> t() {
return Arrays.asList(new Object[][]{
{3, 1, 2},
{4, 1, 3}
}
);
}
public ParameterTest(int expected,int input1,int input2){
this.expected=expected;
this.input1=input1;
this.input2=input2;
}
@Test
public void testAdd(){
Assert.assertEquals(expected,new Calculate().add(input1,input2));
}
}