Java單元測試常用工具類小結

單元測試

單元測試是系統中非常基礎的功能,以功能的最小粒度進行功能測試,保證系統功能的正確行。

Assert

所屬類庫: JUnit library
類名: Assert
功能描述: 用以判斷結果是否符合預期
常用方法:

  • assertTrue(String message, boolean condition)
  • assertThat(String reason, T actual, Matcher<? super T> matcher)
  • assertEquals(String message, Object expected, Object acutal)

由於Assert其中提供了大量的判斷方法,這裏就不再一一贅述,在需要之時進行查閱即可。
這裏以assertThat爲例做一個簡要的分析:
其源代碼定義如下:

public static <T> void assertThat(T actual, Matcher<? super T> matcher) {
       assertThat("", actual, matcher);
   }

其中參數如下:

  • T: 只是判斷的數據類型, 與第三個參數matcher中的T類型相同
  • Matcher: 是hamcrest類庫中的Matcher接口,用來實現基本的判斷比較,稍後將針對Matcher進行簡要的介紹分析
  • reason。自定義的描述下信息,在判斷爲失敗的情況下展示。

使用示例如下:

import org.junit.Assert;
import org.junit.Test;
import static org.hamcrest.number.OrderingComparison.greaterThan;
public class AssertDemo {
    /**
     * 檢查數字值需要大於10
     */
    @Test
    public void testAssert() {
        Long count = 12l;
        Assert.assertThat("Count is lower than 10", count, greaterThan(10l));
    }
}

這裏的單元測試簡單用於測試count的值是否大於10,類型爲Long。

Hamcrest介紹

官方站點: http://hamcrest.org/JavaHamcrest/distributables
在Spring Boot中的單元測試中,其依賴關係如下:
Hamcrest依賴
從其中可以看出,hamcrest存在兩個類庫,其中junit依賴的是hamcrest-core,包括在實際的單元測試中,同樣會碰到在類庫類庫中出現相同的方法。兩者的區別是什麼呢?
Hamcrest類庫進行了拆分,hamcrest-core包括最基本的matchers和抽象類以及創建這些matcher的工廠方法;主要用於構建其它的Matchers。類庫路徑: org.hamcrest.CoreMatchers。
hamcrest-library: 主要按照功能進行分組的Matcher,他們是可選的,擴展的Mather功能。
org.hamcrest.Matchers包含了core和library中兩者的功能。

如何來使用呢?
簡單起見,就直接將它們引入進來即可:

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

所有的這些Matcher是可以彼此嵌套使用的。

Hamcrest用法

定義實體類:

@Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class Apple {
       private Long id;
       private String name;

       @Override
        public String toString() {
           return id + "-" + name;
       }
    }

Core

  • anything - always matches, true
  • describedAs 定製失敗描述信息的裝飾器
  • is 等同於equalTo()

測試代碼:

@Test
    public void testCore() {
        List<String> strs = Lists.newArrayList();
        strs.add("abef");
        strs.add("what it is");

        //無論如何都是成功的
        Assert.assertThat(strs, Matchers.anything());

        List<String> tstrs = Lists.newArrayList();
        tstrs.add("abef");

        // 裝飾模式,定製化錯誤提示信息
        Assert.assertThat(strs, Matchers.describedAs("Custom Failure Information:%0", Matchers.hasItem("1abef"), "Array Item"));
    }

注意這裏的decorateAs方法,其中使用的%0的位置佔位符。

Logical

  • allOf - matches if all matchers match, short circuits (like Java &&)
  • anyOf - matches if any matchers match, short circuits (like Java ||)
  • not - matches if the wrapped matcher doesn’t match and vice versa
  • either(Matcher).or(Matcher)
  • both(Matcher).and(Matcher)

使用代碼示例:

 @Test
    public void testLogic() {
        Apple apl = new Apple();
        Apple apl2 = apl;

        // allOf:如果所有匹配器都匹配才匹配
        Assert.assertThat("What it is?", Matchers.allOf(Matchers.endsWith("?"), Matchers.startsWith("What")));

        // anyOf:如果任何匹配器匹配就匹配
        Assert.assertThat("What it is?", Matchers.anyOf(Matchers.endsWith("?"), Matchers.notNullValue()));

        // not:如果包裝的匹配器不匹配器時匹配,反之亦然
        Assert.assertThat("What it is?", Matchers.not(Matchers.endsWith("is")));

        // is:如果包裝的匹配器匹配器時匹配,反之亦然
        Assert.assertThat(apl, Matchers.is(apl2));
        Assert.assertThat("What it is?", Matchers.is(Matchers.endsWith("is?")));
    }

Object

  • equalTo 測試 object 是否相等的,底層使用Object.equals
  • hasToString 測試Object.toString
  • instanceOf, isCompatibleType 判斷類型
  • notNullValue, nullValue 判斷對象null
  • sameInstance 判斷是否爲同一個對象

使用示例:

 @Test
    public void testObject() {
        Apple apl = new Apple(12l, "name1");
        Apple apl2 = apl;

        //判斷對象是否相等
        Assert.assertThat(apl,  Matchers.equalTo(apl2));

        // has ToString
        //測試toString()
        Assert.assertThat(apl, Matchers.hasToString("12-name1"));

        //InstanceOf
        Assert.assertThat(apl, Matchers.instanceOf(Apple.class));

        //NotnullValue
        Assert.assertThat(apl, Matchers.notNullValue());

        //NullValue
        Assert.assertThat(null, Matchers.nullValue());

        //same instance
        Assert.assertThat(apl,  Matchers.sameInstance(apl2));
    }

Beans

  • hasProperty 判斷Bean是否特定屬性

代碼使用示例:

 @Test
    public void testBean() {
       Apple apl = new Apple();

       //check 其是否有屬性name
       Assert.assertThat(apl, Matchers.hasProperty("name"));
    }

Collections

這裏的集合是指Matcher集合,不是指數據。

  • array Matcher數據匹配
  • hasEntry, hasKey, hasValue, 檢查Map中是否含有an entry, key or value
  • hasItem, hasItems,測試一個Collections是否含有元素
  • hasItemInArray 檢查數組中是否包含一個元素
  • isIn(T t): 檢查是否在某個Colleciton之內
  • arrayContainingInAnyOrder
  • arrayContaining()
  • arrayWithSize(int/Matcher): 數組大小
  • hasSize(int/Mathcer): Collection大小

使用示例:

@Test
    public void testCollection() {
        List<Apple> apples = Lists.newArrayList();
        apples.add(new Apple(1l, "zhangsan"));
        apples.add(new Apple(2l, "lisi"));

        Apple[] aplArray = new Apple[]{new Apple(3l, "zhangsan"), new Apple(4l, "wangwu")};

        Apple apl = new Apple(1l, "zhagnsan");
        Apple testApl = apples.get(0);

        String[] strArray = {"12", "34"};
        //注意這裏只能是Object Array
        Assert.assertThat(apples.toArray(aplArray), Matchers.array(Matchers.notNullValue(), Matchers.hasProperty( "name")));
        Assert.assertThat(strArray, Matchers.array(Matchers.equalTo("12"), Matchers.equalTo("34")));

        Map<String, String> dataMap = new HashMap();
        dataMap.put("key1", "val1");
        dataMap.put("key2", "val2");

        Assert.assertThat(dataMap, Matchers.hasEntry("key1", "val1"));
        Assert.assertThat(dataMap, Matchers.hasValue("val1"));
        Assert.assertThat(dataMap, Matchers.hasKey("key1"));
    }
    
@Test
    public void testIterable() {
        List<Apple> apples = Lists.newArrayList();
        apples.add(new Apple(1l, "zhangsan"));
        apples.add(new Apple(2l, "lisi"));

        Apple apl = new Apple(1l, "zhangsan");
        Apple apl1 = new Apple(2l, "lisi");

        //檢查單個元素
        Assert.assertThat(apples, Matchers.hasItem(apl));

        //檢查多個元素
        Assert.assertThat(apples, Matchers.hasItems(apl1,apl));

        Apple[] aplArray = new Apple[2];
        //數組元素
        Assert.assertThat(apples.toArray(aplArray), Matchers.hasItemInArray(apl));

        //在Colleciton中
        Assert.assertThat(apl, Matchers.isIn(apples));
    }

Number

  • closeTo 測試浮點數是否接近一個數字值
  • greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo 檢查數字的大小

使用示例代碼:

 @Test
    public void testNumber() {
        // closeTo:測試浮點值接近給定的值
        Assert.assertThat(1.5, Matchers.closeTo(1.0, 0.6));
        // greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo:測試大於,小於
        Assert.assertThat(1.0, Matchers.greaterThan(0.5));
        Assert.assertThat(1.5, Matchers.lessThanOrEqualTo(1.5));
    }

Text

  • equalToIgnoringCase 檢查字符串相等,忽略大小寫
  • equalToIgnoringWhiteSpace 檢查字符串相等,忽略空白字符
  • containsString, endsWith, startsWith 檢查字符串匹配

使用示例:

@Test
    public void testText() {
        // equalToIgnoringCase:測試字符串相等忽略大小寫
        Assert.assertThat("Hello world", Matchers.equalToIgnoringCase("hello world"));
        // equalToIgnoringWhiteSpace:測試字符串忽略空白
        Assert.assertThat("  Hello world", Matchers.equalToIgnoringWhiteSpace("Helloworld"));
        // containsString, endsWith, startsWith:測試字符串匹配
        Assert.assertThat("Hello world", Matchers.containsString("Hello"));
        Assert.assertThat("Hello world", Matchers.startsWith("Hello"));
        Assert.assertThat("Hello world", Matchers.endsWith("world"));
    }

總結

這裏所有的這些方法在語言中都是有其他替代選擇,他們只是讓你閱讀起來更容易而已,更符合人的閱讀和理解習慣。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章