欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

hamcrest详细介绍

发布时间:2024/4/13 72 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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
IsAnythingalways 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>大小比较
StringContainsInOrderString集合是否都以…开头
SubstringMatcher子串匹配
ThrowableMessageMatcher异常信息匹配

ReflectiveTypeFinder

此类找到某个类包含某个方法的superclass,从下往上查找。

类型安全诊断匹配器

名称用途说明
CombinableMatcher组合匹配器and,or,
Every每个元素都匹配
HasPropertyWithValue属性值匹配
HasXPathxml匹配
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详细介绍的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。