目錄
1. 開胃示例
2. 固件測試
3. 忽略測試用例
4. 異常測試
5. 逾時測試
6. 測試運作器
7. 參數化測試
8. 套件測試
9. JUnit4.4assertThat斷言
10. assumeThat斷言
11. 參考
一、開胃示例
被測試類——擷取學生資料:
/**
* 學生資訊實體
*/
public class EStudent{
String name;
int age;
// 一堆getter、setter
/**
* 注意:自定義類時要重寫equals方法,否則預設equals方法會使用==方式來比較對象的記憶體位址是否相同,而不是内容是否相同。
*/
@Override
public boolean equals(Object obj){
if (obj == null || obl.getClass() != this.getClass()) return false;
EStudent other = (EStudent)obj;
if (!this.getName().equals(other.getName())
|| this.getAge() != other.getAge()) return false;
return true;
}
}
/**
* 學生資料操作接口
*/
public interface StudentDao{
public EStudent getStudent(int id);
}
/**
* 真實的資料操作類
*/
public class StudentDaoImpl
implements StudentDao{
public EStudent getStudent(int id){
EStudent ret = new EStudent();
ret.setName("fsjohnhuang");
ret.setAge(18);
return ret;
}
}
/**
* 模拟的資料操作類
*/
public class MockStudentDaoImpl
implements StudentDao{
public EStudent getStudent(int id){
EStudent ret = new EStudent();
ret.setName("fsjohnhuang");
ret.setAge(18);
return ret;
}
}
測試用例:
/**
* 由于下面采用參數化測試,是以将測試運作器更換為Parameterized
*/
@RunWith(Parameterized.class)
public class TestStudentDao{
static StudentDao dao, mockDao;
/**
* 執行測試類中所有測試前執行一次
* 用于前提條件的初始化
*/
@BeforeClass
public static void init(){
dao = new StudentDaoImpl();
mockDao = new MockStudentDaoImpl();
}
/**
* 接收測試用資料
*/
int id;
public TestStudentDao(int id){
this.id = id;
}
/**
* 測試用例
*/
@Test
public void testGetStudent(){
assertEquals("擷取學生資訊", mockDao.getStudent(id), dao.getStudent(id));
}
/**
* 測試用資料提供方法
*/
@Parameters
public Collection dataFeed(){
return Arrays.asList(new Object[][]{{1},{2},{3}});
}
}
第一次接觸時可能不能了解上面的代碼片段到底發生了什麼事,ctrl+c到eclipse那執行一下吧!下面我們詳細了解JUnit4吧!
二、固件測試
就是每個測試方法執行前和後都執行的方法,用于自動初始化和回收資源等工作。通過 @Before 注解标注測試方法執行前調用的固件測試,通過 @After 注解标注測試方法執行後調用的固件測試。
父類的固件測試會在子類的固件測試前被調用。
另外可通過注解 @BeforeClass 和 @AfterClass 标注某些static方法為測試開始前和結束後被調用,用于自動初始化和回收資源等工作。注意通過注解 @BeforeClass 和 @AfterClass 标注的方法一次測試過程僅被調用一次而已。示例如下:
public class MyUT{
int i, j;
@BeforeClass public void static init(){
System.out.println("init");
i = 0;
j = 0;
}
@Before public void invokeBefore(){
System.out.println("invokeBefore" + ++i);
}
@Test public void testMyMethod1(){
System.out.println("testMyMethod1");
}
@Test public void testMyMethod2(){
System.out.println("testMyMethod2");
}
@After public void invokeAfter(){
System.out.println("invokeAfter" + ++j);
}
@AfterClass public void static destroy(){
System.out.println("destroy");
i = 0;
j = 0;
}
}
// 輸出結果
init
invokeBefore1
testMyMethod1
invokeAfter1
invokeBefore2
testMyMethod2
invokeAfter2
destroy
三、忽略測試用例
通過注解 @Ignore() 可以标注不參與測試的測試方法。當然也可以通過去除注解 @Test 來達到這個目的,但去除注解 @Test 會令到eclipse的JUnit View中無法顯示該測試方法。
四、異常測試
通過注解 @Test(expected=Class類型) 來标注期待測試方法執行時抛出哪種異常對象,若測試方法不抛出異常或異常對象與期待的異常對象不相同則測試失敗。
@Test(expected=ArithmeticException.class)
public void calc(){
int i = 1/0;
}
五、逾時測試
通過注解 @Test(timeout=毫秒數) 來标注期待執行測試方法的最大耗時,若逾時則測試失敗。
@Test(timeout=1000)
public void wait(){
while(true){}
}
六、測試運作器
用于執行JUnit中所有的測試方法。JUnit為單元測試提供預設的測試運作器,但我們可以自定義,自定義的測試運作器必須繼承 org.junit.runner.Runner 。然後通過類注解 @RunWith(CustomTestRunner.class) 來指定該測試的測試運作器。
常用的内置測試運作器:
1. org.junit.runners.Suite ,套件測試時使用。
2. org.junit.runners.Parameterized ,參數化測試時使用。
七、參數化測試
就是第一節中的測試類型,用于模拟以不同的參數組合來對方法進行多次測試。若不使用參數化測試,該測試方法有N個不同的參數組合,則需要寫N個測試方法來測試。
// 需要使用Parameterized測試運作器才可以
@RunWith(Parameterized.class)
public class MyUT{
// 成員變量,用于存放測試用資料和測試期望值
int orig;
int expected;
public MyUT(int orig, int expected){
this.orig = orig;
this.expected = expected;
}
@Test public void testMyMethod(){
assertEquals(expected, orig + 1);
}
/**
* 測試資料和測試期望值的提供方法
* 必須用注解@Parameters标注
* 必須傳回Collection類型資料
*/
@Parameters public Collection dataFeed(){
return Arrays.asList(new Object[][]{
{1, 2},
{2, 3},
{3, 4}
});
}
}
八、套件測試
JUnit4去除JUnit3中套件測試注解,而是通過另一形式提供套件測試。
套件測試就是按業務邏輯将測試類進行分組,并以組為機關執行單元測試。
// 測試類1
public class MyUT1{
@Test public void testMyMehthod1(){
assertEquals(1,1);
}
}
// 測試類2
public class MyUT2{
@Test public void testMyMehthod2(){
assertEquals(2,2);
}
}
// 套件測試類
@RunWith(Suite.class)
@SuiteClass({MyUT1.class, MyUT2.class})
public class SuiteTest{
// 必須有一個public,無參數的構造函數。使用預設的構造函數也可以
public SuiteTest(){}
}
九、JUnit4.4的 assertThat斷言
JUnit4.4内置Hamcrest測試元件的部分内容,而 assertThat斷言 就是配置Hamcrest測試元件的比對符來實作所有測試工作。由于Hamcrest的比對符貼近自然語言,是以意思表達更明确。(JUnit4.4前的版本則需要引入hamcrest-core.jar和hamcrest-library.jar了)。
/* assertThat文法
* assertThat(T actual, Matcher<T> matcher);
* assertThat(String reason, T actual, Matcher<T> matcher);
* 入參actual為實際值,入參matcher為期待值的比對符
*/
//測試變量是否大于指定值
assertThat(1, greaterThan(50));
//測試變量是否小于指定值
assertThat(1, lessThan(100));
//測試變量是否大于等于指定值
assertThat(1, greaterThanOrEqualTo(50));
//測試變量是否小于等于指定值
assertThat(1, lessThanOrEqualTo(100));
//測試所有條件必須成立
assertThat(1, allOf(greaterThan(50),lessThan(100)));
//測試隻要有一個條件成立
assertThat(1, anyOf(greaterThanOrEqualTo(50), lessThanOrEqualTo(100)));
//測試無論什麼條件成立(還沒明白這個到底是什麼意思)
assertThat(1, anything());
//測試變量值等于指定值
assertThat(1, is(100));
//測試變量不等于指定值
assertThat(1, not(50));
/**字元串**/
String url = "http://www.taobao.com";
//測試變量是否包含指定字元
assertThat(url, containsString("taobao"));
//測試變量是否已指定字元串開頭
assertThat(url, startsWith("http://"));
//測試變量是否以指定字元串結尾
assertThat(url, endsWith(".com"));
//測試變量是否等于指定字元串
assertThat(url, equalTo("http://www.taobao.com"));
//測試變量再忽略大小寫的情況下是否等于指定字元串
assertThat(url, equalToIgnoringCase("http://www.taobao.com"));
//測試變量再忽略頭尾任意空格的情況下是否等于指定字元串
assertThat(url, equalToIgnoringWhiteSpace("http://www.taobao.com"));
/**集合**/
List<User> user = new ArrayList<User>();
user.add(test1);
user.add(test2);
//測試集合中是否還有指定元素
assertThat(user, hasItem(test1));
assertThat(user, hasItem(test2));
/**Map比對**/
Map<String,User> userMap = new HashMap<String,User>();
userMap.put(test1.getUsername(), test1);
userMap.put(test2.getUsername(), test2);
//測試map中是否還有指定鍵值對
assertThat(userMap, hasEntry(test1.getUsername(), test1));
//測試map中是否還有指定鍵
assertThat(userMap, hasKey(test2.getUsername()));
//測試map中是否還有指定值
assertThat(userMap, hasValue(test2));
十、 assumeThat假設斷言
位于 org.junit.Assume類 下,同樣是屬于Hamcrest元件的。用于假設當條件成立時才會執行後續的代碼,條件不成立時是不會影響測試結果。
assumeThat(1, is(0));
System.out.println("I'm here"); // 這句不會被執
尊重原創,轉載請注明來自:http://www.cnblogs.com/fsjohnhuang/p/4061902.html ^_^肥仔John
十一、參考
http://blog.csdn.net/skyie53101517/article/details/8739126
歡迎添加我的公衆号一起深入探讨技術手藝人的那些事!
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5CO2gjMwcDO4ITMtkjMzETN1UzNxcTMyEDMyAjMtIDMwcDNz8CXyEDMyAjMvwlMwAzN0MzLcd2bsJ2Lc12bj5ycn9Gbi52YuAjMwIzZtl2Lc9CX6MHc0RHaiojIsJye.png)
如果您覺得本文的内容有趣就掃一下吧!捐贈互勉!