簡單的Junit4應用

2021-08-31 20:42:29 字數 3535 閱讀 5446

如果寫了乙個類,想測試一下有沒有bug,可以用main方法去測試。但是main方法缺點很多,不是理想的做單元測試的途徑——方法不能一起執行,且測試結果多數要通過程式設計師自己觀察才可以判定。

為了克服這些缺點,使單元測試更加簡單方便,junit是乙個很好的選擇。接下來,將會講解下junit4的使用。

要使用junit4非常的簡單,準備工作也非常方便。以junit4.8.1為例,只需要新增junit-4.8.1.jar就可以使用junit的所有傳統方法。

以下是乙個小例子:

待測試類t

package com.ellis.junit4;  

public class t

}

package com.ellis.junit4.test;  

import static org.junit.assert.*;

import org.junit.test;

import com.ellis.junit4.t;

public class ttest

}

ttest.class裡的testadd方法用來測試t.class裡的add方法。@test代表這個方法為測試方法。如果有多個方法用@test注釋的話,那麼多個方法中的每個方法都可以單獨執行(執行乙個方法的時候,不執行其它方法),這是用main方法測試所無法比擬的。執行單個@test方法的操作是:選中該方法名,然後點run。

ttest.class裡的assertequals()就是junit的斷言方法之一,它的作用是判斷z的值是否為8,如果z不等於8的話junit將會報錯,"z !=8"是自己寫的用於與該錯誤提示一起提示出來字串,使錯誤原因更加直觀。assert*()方法有很多,它們都是org.junit.assert的靜態方法,若要像ttest.class裡那樣使用,需要進行靜態引入:import static org.junit.assert.*。

傳統的,也就是junit3及其之前的assert*()方法有很多,在這裡就不一一介紹。這裡主要介紹一下junit4裡新增加的hamcrest斷言assertthat()。

若要使用hamcrest的斷言,還必須新增hamcrest-core-1.2.jar和hamcrest-library-1.2.jar這個兩個jar包。

assertthat()有什麼作用?使用assertthat()有什麼好處呢?assertthat()比起傳統的assert*()斷言,在功能上並沒有多大的不同,它最主要的優點是在於——1.它的語法更接近於口語,從而使**寫和看起來更加直觀;2.乙個assertthat()可以實現絕大部分常用的傳統assert*()方法的功能。下面以用assertthat()代替assertequals()為例,講解一下assertthat()。

用assertthat()的ttest:

package com.ellis.junit4.test;  

import static org.junit.assert.*;

import static org.hamcrest.matchers.*;

import org.junit.test;

import com.ellis.junit4.t;

public class ttest

}

如上面**所示assertthat(z, is(8))的作用是判斷z的值是否為8(z is 8 or not ?),它的作用和assertequals(z, 8)完全一樣。is()其實是org.hamcrest.matchers的靜態方法,若要如此使用必須先進行靜態引入import static org.hamcrest.matchers.*。

上面**中的assertthat(z, allof(greaterthan(5), lessthan(10)))的作用是判斷z是否都allof()裡面的條件,allof()裡的條件是大於5(greaterthan(5))、小於3(lessthan(10))。

assertthat()常用的方法還有:

a)assertthat( n, allof( greaterthan(1), lessthan(15) ) ); n滿足allof()裡的所有條件

assertthat( n, anyof( greaterthan(16), lessthan(8) ) );n滿足anyof()裡的任意條件

assertthat( n, anything() );  n是任意值(任意值都可以通過測試)

assertthat( str, is( "ellis" ) ); str是is()裡的內容

assertthat( str, not( "ellis" ) ); str不是not()裡的內容

b)assertthat( str, containsstring( "ellis" ) ); str包含containsstring()裡的內容

assertthat( str, endswith("ellis" ) );  str以endswith()裡的內容結尾

assertthat( str, startswith( "ellis" ) ); str以startswith()裡的內容開始

assertthat( n, equalto( nexpected ) ); n與equalto()裡的內容相等

assertthat( str, equaltoignoringcase( "ellis" ) ); str忽略大小寫後與equaltoignoringcase()裡的內容相等

assertthat( str, equaltoignoringwhitespace( "ellis" ) );str忽略空格後與equaltoignoringwhitespace()裡的內容相等

c)assertthat( d, closeto( 3.0, 0.3 ) );d接近於3.0,誤差不超過0.3

assertthat( d, greaterthan(3.0) );d大於3.0

assertthat( d, lessthan (10.0) );d小於10.0

assertthat( d, greaterthanorequalto (5.0) );d大於或等於5.0

assertthat( d, lessthanorequalto (16.0) );d小於或等於16.0

d)assertthat( map, hasentry( "ellis", "ellis" ) );map裡有乙個名為ellis的key,其值為ellis

assertthat( iterable, hasitem ( "ellis" ) );iterable(例如list)裡包含值ellis

assertthat( map, haskey ( "ellis" ) );map有乙個名為ellis的key

assertthat( map, hasvalue ( "ellis" ) );map裡包含乙個值ellis

另外,還有如下這些常用註解,使測試起來更加方便:

1.被忽略的測試方法 2.

每乙個測試方法之前執行 3.

每乙個測試方法之後執行 4.

所有測試開始之前執行 5.

所有測試結束之後執行

Junit4學習(五)Junit4測試套件

一,背景 1,隨著開發規模的深入和擴大,專案或越來越大,相應的我們的測試類也會越來越多 那麼就帶來乙個問題,假如測試類很多,就需要多次執行,造成測試的成本增加 此時就可以使用junit批量執行測試類的功能,junit test suite,測試套件 每次執行測試類,只需要執行一次測試套件類就可以執行...

JUnit4常用注釋

常用注釋 解釋 test 定義乙個要測試的方法 before 在每乙個測試之前,都會被執行的方法,這個方法常常被用來進行一些測試環境的初始化 after 與 before進行對應,在測試結束後,做一些清理工作 beforeclass 在所有測試開始之前執行,這個方法在類執行的時候執行,而且只會執行一...

junit4常用註解

test 將乙個普通的方法修飾成乙個測試方法 test expected exception.class 若測試時改方法丟擲 exception異常則測試成功 test timeout 毫秒 超出該時間則停止執行 beforeclass 它會在該測試類的所有方法執行前執行,static修飾,只執行一...