JUnit(三)高级之Matchers and assertThat_MD

来源:互联网 发布:淘宝商城铂金会员 编辑:程序博客网 时间:2024/05/18 07:03

一、assertThat

参考: assertThat

二、JUnit Matchers

  • Javadoc JUnitMatchers http://junit.org/junit4/javadoc/latest/org/junit/matchers/JUnitMatchers.html

该比较器中大部分都被标记为Deprecated,并使用org.hamcrest.CoreMatchers对应方法进行取代,但有两个方法得到了保留:

  • static <T extends Exception> Matcher<T> isException(Matcher<T> exceptionMatcher)
  • static <T extends Throwable> Matcher<T> isThrowable(Matcher<T> throwableMatcher)

暂不理解其使用场景,待后期深入。。。。。

三、Hamcrest CoreMatchers

Hamcrest CoreMatchers在JUnit4.9版本被包含在JUnit的分发包中。

  • JavaDoc Hamcrest CoreMatchers http://junit.org/junit4/javadoc/latest/org/hamcrest/CoreMatchers.html

Hamcrest comes with a library of useful matchers. Here are some of the most important ones.

  • Core
    • anything - 总是匹配,如果你不关心测试下的对象是什么是有用的
    • describedAs - decorator to adding custom failure description[添加一个定制的失败表述装饰器]
    • is - decorator to improve readability[改进可读性装饰器]
  • Logical
    • allOf - 如果所有匹配器都匹配才匹配, short circuits (like Java &&)
    • anyOf - 如果任何匹配器匹配就匹配, short circuits (like Java ||)
    • not - 如果包装的匹配器不匹配器时匹配,反之亦然
  • Object
    • equalTo - 使用 Object.equals 方法来测试对象相等
    • hasToString - 测试Object.toString方法
    • instanceOf, isCompatibleType - 测试类型
    • notNullValue, nullValue - 测试null
    • sameInstance - 测试对象实例
  • Beans
    • hasProperty - 测试JavaBeans属性
  • Collections
    • array - test an array’s elements against an array of matchers[测试数组元素在数组的匹配]
    • hasEntry, hasKey, hasValue - 测试一个Map包含一个实体,键或者值
    • hasItem, hasItems - 测试一个集合包含一个元素
    • hasItemInArray - 测试一个数组包含一个元素
  • Number
    • closeTo - 测试浮点值接近给定的值
    • greaterThan, greaterThanOrEqualTo, lessThan, lessThanOrEqualTo - 测试次序
  • Text
    • equalToIgnoringCase - 测试字符串相等忽略大小写
    • equalToIgnoringWhiteSpace - 测试字符串忽略空白
    • containsString, endsWith, startsWith - 测试字符串匹配

示例

public class C {    public int add(int a, int b) {        return a + b;    }    public double div(double a, double b) {        return a / b;    }    public String getName(String name) {        return name;    }    public List<String> getList(String item) {        List<String> l = new ArrayList<String>();        l.add(item);        return l;    }    public Map<String, String> getMap(String key, String value) {        Map<String, String> m = new HashMap<String, String>();        m.put(key, value);        return m;    }}
@Testpublic void testC() {    // 一般匹配符    int s = new C().add(1, 1);    // allOf:所有条件必须都成立,测试才通过    assertThat(s, allOf(greaterThan(1), lessThan(3)));    // anyOf:只要有一个条件成立,测试就通过    assertThat(s, anyOf(greaterThan(1), lessThan(1)));    // anything:无论什么条件,测试都通过    assertThat(s, anything());    // is:变量的值等于指定值时,测试通过    assertThat(s, is(2));    // not:和is相反,变量的值不等于指定值时,测试通过    assertThat(s, not(1));    // 数值匹配符    double d = new C().div(10, 3);    // closeTo:浮点型变量的值在3.0±0.5范围内,测试通过    assertThat(d, closeTo(3.0, 0.5));    // greaterThan:变量的值大于指定值时,测试通过    assertThat(d, greaterThan(3.0));    // lessThan:变量的值小于指定值时,测试通过    assertThat(d, lessThan(3.5));    // greaterThanOrEuqalTo:变量的值大于等于指定值时,测试通过    assertThat(d, greaterThanOrEqualTo(3.3));    // lessThanOrEqualTo:变量的值小于等于指定值时,测试通过    assertThat(d, lessThanOrEqualTo(3.4));    // 字符串匹配符    String n = new C().getName("Magci");    // containsString:字符串变量中包含指定字符串时,测试通过    assertThat(n, containsString("ci"));    // startsWith:字符串变量以指定字符串开头时,测试通过    assertThat(n, startsWith("Ma"));    // endsWith:字符串变量以指定字符串结尾时,测试通过    assertThat(n, endsWith("i"));    // euqalTo:字符串变量等于指定字符串时,测试通过    assertThat(n, equalTo("Magci"));    // equalToIgnoringCase:字符串变量在忽略大小写的情况下等于指定字符串时,测试通过    assertThat(n, equalToIgnoringCase("magci"));    // equalToIgnoringWhiteSpace:字符串变量在忽略头尾任意空格的情况下等于指定字符串时,测试通过    assertThat(n, equalToIgnoringWhiteSpace(" Magci   "));    // 集合匹配符    List<String> l = new C().getList("Magci");    // hasItem:Iterable变量中含有指定元素时,测试通过    assertThat(l, hasItem("Magci"));    Map<String, String> m = new C().getMap("mgc", "Magci");    // hasEntry:Map变量中含有指定键值对时,测试通过    assertThat(m, hasEntry("mgc", "Magci"));    // hasKey:Map变量中含有指定键时,测试通过    assertThat(m, hasKey("mgc"));    // hasValue:Map变量中含有指定值时,测试通过    assertThat(m, hasValue("Magci"));}

四、Thirdparty Matchers

Other, potentially Matchers out there include

  • Excel spreadsheet matchers
  • JSON matchers
  • XML/XPath matchers

赞赏码