hamcrest详细介绍
使用过Junit 的应该有过体验:在实际开发中,一些基本的断言,如eqaul,null,true它们的可读性并不是很好。而且很多时候我们要比较对象、集合、Map等数据结构。这样我们要么进行大段的字段获取再断言。或者干脆自己编写表达式并断言其结果。
JUnit4.4引入了Hamcrest框架,Hamcest提供了一套匹配符Matcher,这些匹配符更接近自然语言,可读性高,更加灵活。
Hamcrest 提供了大量被称为“匹配器”的方法。其中每个匹配器都设计用于执行特定的比较操作。Hamcrest的可扩展性很好,让你能够创建自定义的匹配器。最重要的是,JUnit也包含了Hamcrest的核心,提供了对Hamcrest的原生支持,可以直接使用Hamcrest。
类图
类实现
hamcrest架构中,主要实现匹配器(Matcher),因此Matcher作为顶层类型。
Matcher
public interface Matcher<T> extends SelfDescribing {//验证对象是否匹配。此处用Object,因为在运行时不知道是什么类型,由实现类决定boolean matches(Object item);//**构造不匹配的缘由。**void describeMismatch(Object item, Description mismatchDescription); }BaseMatcher
BaseMatcher作为所有匹配器的基类,不建议直接实现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 | 仅用于作为匿名内部类的父类 | |
| DescribedAs | 为其他Matcher提供一个Description | |
| AllOf | 判断多个Mathcer是否同时true。短路方式 | |
| IsInstanceOf | 判断是否是指定class的实例 | |
| PropertyMatcher | 判断属性值是否匹配 | |
| Is | 装饰一个其他Matcher | |
| IsAnything | always return true | |
| IsEmptyString | 判断不为空字符串 | |
| IsEqual | 判断是否equal | |
| IsIn | 是否是集合元素 | |
| IsNot | 装饰一个其他Matcher,判断Matcher不匹配 | |
| IsNull | 判断对象是否为null | |
| IsSame | 判断对象是同一个 | |
| AnyOf | 判断多个Mathcer是否有一个true。短路方式 | |
| TypeSafeMatcher | 类型安全匹配器。把一个类方便的转化为一个Matcher | |
| TypeSafeDiagnosingMatcher | 类型安全诊断匹配器。把一个类方便的转化为一个Matcher,并报告为什么不匹配。 |
类型安全匹配器
| BigDecimalCloseTo | 是否近似某个值 | |
| HasProperty | 是否有属性 | |
| IsArray | 集合是否一一匹配一组Matcher | |
| IsArrayContaining | 集合是否有一个元素匹配 | |
| IsCloseTo | 是否近似某个值 | |
| IsCompatibleType | 一个类是否是另一个类的子类 | |
| IsEmptyCollection | 是否是一个空集合 | |
| IsEmptyIterable | 迭代是否是空的 | |
| IsEqualIgnoringCase | 是否相同,忽略大小写 | |
| IsEqualIgnoringWhiteSpace | 是否相同,忽略空白 | |
| IsMapContaining<K,V> | Map是否有一个元素匹配 | |
| OrderingComparison<T extends Comparable> | 大小比较 | |
| StringContainsInOrder | String集合是否都以…开头 | |
| SubstringMatcher | 子串匹配 | |
| ThrowableMessageMatcher | 异常信息匹配 | |
ReflectiveTypeFinder
此类找到某个类包含某个方法的superclass,从下往上查找。
类型安全诊断匹配器
| CombinableMatcher | 组合匹配器 | and,or, |
| Every | 每个元素都匹配 | |
| HasPropertyWithValue | 属性值匹配 | |
| HasXPath | xml匹配 | |
| IsCollectionContaining | 有集合元素匹配 | |
| IsEventFrom | 事件源是同一个对象 | |
| SamePropertyValuesAs | ||
| IsIterableContainingInAnyOrder | ||
| IsIterableContainingInOrder | ||
| FeatureMatcher<T, U> |
Dsecription
描述不匹配情况的类。每个Matcher描述自己的匹配情况。
NullDescription
丢弃信息
BaseDescription
基础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);} }结果:
anything:true isEmptyString:false isEqual:true isNull:false isEqual:true is:true isNot:false isInstanceOf:true allOf:true anyOf:true hasProperty:true hasPropertyWithValue:true注意:与属性有关的Matcher,必须保证测试类是public的,方法是public的。
总结
以上是生活随笔为你收集整理的hamcrest详细介绍的全部内容,希望文章能够帮你解决所遇到的问题。