hamcrest详细介绍
使用過Junit 的應(yīng)該有過體驗(yàn):在實(shí)際開發(fā)中,一些基本的斷言,如eqaul,null,true它們的可讀性并不是很好。而且很多時(shí)候我們要比較對象、集合、Map等數(shù)據(jù)結(jié)構(gòu)。這樣我們要么進(jìn)行大段的字段獲取再斷言。或者干脆自己編寫表達(dá)式并斷言其結(jié)果。
JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,這些匹配符更接近自然語言,可讀性高,更加靈活。
Hamcrest 提供了大量被稱為“匹配器”的方法。其中每個(gè)匹配器都設(shè)計(jì)用于執(zhí)行特定的比較操作。Hamcrest的可擴(kuò)展性很好,讓你能夠創(chuàng)建自定義的匹配器。最重要的是,JUnit也包含了Hamcrest的核心,提供了對Hamcrest的原生支持,可以直接使用Hamcrest。
類圖
類實(shí)現(xiàn)
hamcrest架構(gòu)中,主要實(shí)現(xiàn)匹配器(Matcher),因此Matcher作為頂層類型。
Matcher
public interface Matcher<T> extends SelfDescribing {//驗(yàn)證對象是否匹配。此處用Object,因?yàn)樵谶\(yùn)行時(shí)不知道是什么類型,由實(shí)現(xiàn)類決定boolean matches(Object item);//**構(gòu)造不匹配的緣由。**void describeMismatch(Object item, Description mismatchDescription); }BaseMatcher
BaseMatcher作為所有匹配器的基類,不建議直接實(shí)現(xiàn)Matcher接口。
public abstract class BaseMatcher<T> implements Matcher<T> {@Overridepublic void describeMismatch(Object item, Description description) {description.appendText("was ").appendValue(item);}@Overridepublic String toString() {return StringDescription.toString(this);} }具體匹配器
| CustomMatcher | 僅用于作為匿名內(nèi)部類的父類 | |
| DescribedAs | 為其他Matcher提供一個(gè)Description | |
| AllOf | 判斷多個(gè)Mathcer是否同時(shí)true。短路方式 | |
| IsInstanceOf | 判斷是否是指定class的實(shí)例 | |
| PropertyMatcher | 判斷屬性值是否匹配 | |
| Is | 裝飾一個(gè)其他Matcher | |
| IsAnything | always return true | |
| IsEmptyString | 判斷不為空字符串 | |
| IsEqual | 判斷是否equal | |
| IsIn | 是否是集合元素 | |
| IsNot | 裝飾一個(gè)其他Matcher,判斷Matcher不匹配 | |
| IsNull | 判斷對象是否為null | |
| IsSame | 判斷對象是同一個(gè) | |
| AnyOf | 判斷多個(gè)Mathcer是否有一個(gè)true。短路方式 | |
| TypeSafeMatcher | 類型安全匹配器。把一個(gè)類方便的轉(zhuǎn)化為一個(gè)Matcher | |
| TypeSafeDiagnosingMatcher | 類型安全診斷匹配器。把一個(gè)類方便的轉(zhuǎn)化為一個(gè)Matcher,并報(bào)告為什么不匹配。 |
類型安全匹配器
| BigDecimalCloseTo | 是否近似某個(gè)值 | |
| HasProperty | 是否有屬性 | |
| IsArray | 集合是否一一匹配一組Matcher | |
| IsArrayContaining | 集合是否有一個(gè)元素匹配 | |
| IsCloseTo | 是否近似某個(gè)值 | |
| IsCompatibleType | 一個(gè)類是否是另一個(gè)類的子類 | |
| IsEmptyCollection | 是否是一個(gè)空集合 | |
| IsEmptyIterable | 迭代是否是空的 | |
| IsEqualIgnoringCase | 是否相同,忽略大小寫 | |
| IsEqualIgnoringWhiteSpace | 是否相同,忽略空白 | |
| IsMapContaining<K,V> | Map是否有一個(gè)元素匹配 | |
| OrderingComparison<T extends Comparable> | 大小比較 | |
| StringContainsInOrder | String集合是否都以…開頭 | |
| SubstringMatcher | 子串匹配 | |
| ThrowableMessageMatcher | 異常信息匹配 | |
ReflectiveTypeFinder
此類找到某個(gè)類包含某個(gè)方法的superclass,從下往上查找。
類型安全診斷匹配器
| CombinableMatcher | 組合匹配器 | and,or, |
| Every | 每個(gè)元素都匹配 | |
| HasPropertyWithValue | 屬性值匹配 | |
| HasXPath | xml匹配 | |
| IsCollectionContaining | 有集合元素匹配 | |
| IsEventFrom | 事件源是同一個(gè)對象 | |
| SamePropertyValuesAs | ||
| IsIterableContainingInAnyOrder | ||
| IsIterableContainingInOrder | ||
| FeatureMatcher<T, U> |
Dsecription
描述不匹配情況的類。每個(gè)Matcher描述自己的匹配情況。
NullDescription
丟棄信息
BaseDescription
基礎(chǔ)Description,存儲為字符串
StringDescription
繼承于BaseDescription。存儲為String。
Sample
public class Person {String name;int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Person(String name){this.name =name;} } public class HamcrestTest {public static void main(String[] args){Person person = new Person("tom");//IsAnything<Person> anything = new IsAnything<>();boolean matched = anything.matches(person);System.out.println("anything:" + matched);IsEmptyString isEmptyString = new IsEmptyString();matched = isEmptyString.matches("555");System.out.println("isEmptyString:" + matched);IsEqual<Person> isEqual = new IsEqual<Person>(person);matched = isEqual.matches(person);System.out.println("isEqual:" + matched);IsNull<Person> isNull = new IsNull<>();matched = isNull.matches(person);System.out.println("isNull:" + matched);IsSame<Person> isSame = new IsSame<Person>(person);matched = isSame.matches(person);System.out.println("isEqual:" + matched);Is<Person> is = new Is<>(isEqual);matched = is.matches(person);System.out.println("is:" + matched);IsNot<Person> isNot = new IsNot<>(isEqual);matched = isNot.matches(person);System.out.println("isNot:" + matched);IsInstanceOf isInstanceOf = new IsInstanceOf(person.getClass());matched = isInstanceOf.matches(person);System.out.println("isInstanceOf:" + matched);List<Matcher<? super Person>> matchers = new ArrayList<>();matchers.add(is);matchers.add(isSame);AllOf<Person> allOf = new AllOf<Person>(matchers);matched = allOf.matches(person);System.out.println("allOf:" + matched);AnyOf<Person> anyOf = new AnyOf<Person>(matchers);matched = anyOf.matches(person);System.out.println("anyOf:" + matched);HasProperty<Person> hasProperty = new HasProperty<>("name");matched = hasProperty.matches(person);System.out.println("hasProperty:" + matched);HasPropertyWithValue<Person> hasPropertyWithValue = new HasPropertyWithValue<Person>("name",new IsEqual<String>("tom"));matched = hasPropertyWithValue.matches(person);System.out.println("hasPropertyWithValue:" + matched);} }結(jié)果:
anything:true isEmptyString:false isEqual:true isNull:false isEqual:true is:true isNot:false isInstanceOf:true allOf:true anyOf:true hasProperty:true hasPropertyWithValue:true注意:與屬性有關(guān)的Matcher,必須保證測試類是public的,方法是public的。
總結(jié)
以上是生活随笔為你收集整理的hamcrest详细介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。