Hamcrest Common Core Matchers – 哈姆克雷斯特共同核心匹配器

最后修改: 2018年 5月 14日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll explore the CoreMatchers class from the popular Hamcrest framework for writing simple and more expressive test cases.

在这个快速教程中,我们将探讨流行的Hamcrest框架中的CoreMatchers类,以编写简单且更具表现力的测试案例。

The idea is to make assert statements read like natural language.

这个想法是为了使断言语句读起来像自然语言。

2. Hamcrest Setup

2.Hamcrest设置

We can use Hamcrest with Maven by adding the following dependency to our pom.xml file:

我们可以通过在pom.xml文件中添加以下依赖关系来使用Maven的Hamcrest。

<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>java-hamcrest</artifactId>
    <version>2.0.0.0</version>
    <scope>test</scope>
</dependency>

The latest version of this library can always be found here.

这个库的最新版本总是可以在这里找到。

3. Common Core Matchers

3.共同核心匹配器

3.1. is(T) and is(Matcher<T>)

3.1.is(T)/em>和is(Matcher<T>)/em>

The is(T) takes an object as a parameter to check equality and is(Matcher<T>) takes another matcher allowing equality statement to be more expressive.

is(T)将一个对象作为参数来检查平等性,is(Matcher<T>)将另一个匹配器作为参数,允许平等性语句更有表达力。

We can use this with almost all of the methods:

我们可以在几乎所有的方法中使用它

String testString = "hamcrest core";

assertThat(testString, is("hamcrest core"));
assertThat(testString, is(equalTo("hamcrest core")));

3.2. equalTo(T)

3.2.equalTo(T)

The equalTo(T) takes an object as a parameter and checks its equality against another object. This is frequently used with is(Matcher<T>):

equalTo(T)将一个对象作为参数,并检查其与另一个对象是否相等。这经常与is(Matcher<T>)一起使用:

String actualString = "equalTo match";
List<String> actualList = Lists.newArrayList("equalTo", "match");

assertThat(actualString, is(equalTo("equalTo match")));
assertThat(actualList, is(equalTo(Lists.newArrayList("equalTo", "match"))));

We can also use equalToObject(Object operand) – which checks equality and doesn’t enforce that two objects should of same static type:

我们也可以使用equalToObject(Object operand) – ,它检查平等性,并不强制要求两个对象具有相同的静态类型。

Object original = 100;
assertThat(original, equalToObject(100));

3.3. not(T) and not(Matcher<T>)

3.3.not(T)not(Matcher<T>)

The not(T) and not(Matcher<T>) are used to check non-equality of given objects. First takes an object as an argument and second take another matcher:

not(T)not(Matcher<T>)用于检查给定对象的不平等性。前者需要一个对象作为参数,后者需要另一个匹配器。

String testString = "troy kingdom";

assertThat(testString, not("german kingdom"));
assertThat(testString, is(not(equalTo("german kingdom"))));
assertThat(testString, is(not(instanceOf(Integer.class))));

3.4. nullValue() and nullValue(Class<T>)

3.4.nullValue()nullValue(Class<T>)

The nullValue() check for null value against the examined object. The nullValue(Class<T>) checks for nullability of given class type object:

nullValue() 检查被检查对象的null值。nullValue(Class<T>) 检查给定的班级类型对象的无效性。

Integer nullObject = null;

assertThat(nullObject, is(nullValue()));
assertThat(nullObject, is(nullValue(Integer.class)));

3.5. notNullValue() and notNullValue(Class<T>)

3.5.notNullValue()notNullValue(Class<T>)

These are a shortcut to frequently used is(not(nullValue))These check for non-null-equality of an object or with the class type:

这些是经常使用的is(not(nullValue))的一个快捷方式。这些检查对象的非空值或与类的类型。

Integer testNumber = 123;

assertThat(testNumber, is(notNullValue()));
assertThat(testNumber, is(notNullValue(Integer.class)));

3.6. instanceOf(Class<?>)

3.6.instanceOf(Class<?>)

The instanceOf(Class<?>) matches if the examined object is an instance of the specified Class type.

instanceOf(Class<?>) 如果被检查的对象是指定的Class类型的实例,则会匹配。

To verify, this method internally calls the isIntance(Object) of Class class:

为了验证,这个方法在内部调用 isIntance(Object) 类。

assertThat("instanceOf example", is(instanceOf(String.class)));

3.7. isA(Class<T> type)

3.7.isA(Class<T> type)

The isA(Class<T> type) is a shortcut to the above instanceOf(Class<?>). It takes the exact same type of argument as an instanceOf(Class<?>):

isA(Class<T>type)是上述instanceOf(Class<?>)的一个捷径。它接受的参数类型与instanceOf(Class<?>)完全相同。

assertThat("Drogon is biggest dragon", isA(String.class));

3.8. sameInstance()

3.8.sameInstance()

The sameInstance() matches if two reference variables point to the same object in a heap:

sameInstance()如果两个引用变量指向堆中的同一个对象,则匹配。

String string1 = "Viseron";
String string2 = string1;

assertThat(string1, is(sameInstance(string2)));

3.9. any(Class<T>)

3.9.any(Class<T>)

The any(Class<T>)checks if the class is of the same type as actual object:

any(Class<T>)检查该类是否与实际对象属于同一类型。

assertThat("test string", is(any(String.class)));
assertThat("test string", is(any(Object.class)));

3.10. allOf(Matcher<? extends T>…) and anyOf(Matcher<? extends T>…)

3.10.allOf(Matcher<? extends T>…)和anyOf(Matcher<? extends T>…)

We can use allOf(Matcher<? extends T>…) to assert if actual object matches against all of the specified conditions:

我们可以使用allOf(Matcher<? extends T>…)来断言实际对象是否与所有指定条件相匹配。

String testString = "Achilles is powerful";
assertThat(testString, allOf(startsWith("Achi"), endsWith("ul"), containsString("Achilles")));

The anyOf(Matcher<? extends T>…) behaves like allOf(Matcher<? extends T>… ) but matches if the examined object matches any of the specified conditions:

anyOf(Matcher<? extends T>…)的行为与allOf(Matcher<? extends T>…)类似,但如果被检查的对象符合任何指定的条件,就可以匹配。

String testString = "Hector killed Achilles";
assertThat(testString, anyOf(startsWith("Hec"), containsString("baeldung")));

3.11. hasItem(T) and hasItem(Matcher<? extends T>)

3.11.hasItem(T)hasItem(Matcher< ? extends T>)

These match if the examined Iterable collection matches with given object or matcher inside hasItem() or hasItem(Matcher<? extends T>).

如果被检查的Iterable集合与hasItem()hasItem(Matcher<? extends T>)内的给定对象或匹配器相匹配,则这些匹配。

Let’s understand how this works:

让我们了解这一点是如何运作的。

List<String> list = Lists.newArrayList("java", "spring", "baeldung");

assertThat(list, hasItem("java"));
assertThat(list, hasItem(isA(String.class)));

Similarly, we can also assert against more than one items using hasItems(T…) and hasItems(Matcher<? extends T>…):

类似地,我们也可以使用hasItems(T…) hasItems(Matcher< ? extends T> …)对多个项目进行断言。

List<String> list = Lists.newArrayList("java", "spring", "baeldung");

assertThat(list, hasItems("java", "baeldung"));
assertThat(list, hasItems(isA(String.class), endsWith("ing")));

3.12. both(Matcher<? extends T>) and either(Matcher<? extends T>)

3.12.both(Matcher<? extends T>) 和 either(Matcher<? extends T>)

As the name suggests, the both(Matcher<? extends T>) matches when both of the specified conditions match the examined object:

顾名思义,both(Matcher<? extends T>) 当两个指定的条件都与被检查的对象相匹配时,就会匹配。

String testString = "daenerys targaryen";
assertThat(testString, both(startsWith("daene")).and(containsString("yen")));

and either(Matcher<? extends T>)matches when either of the specified conditions matches the examined object:

either(Matcher<?extends T>)当指定的条件中的任何一个与被检查的对象匹配时,就会匹配。

String testString = "daenerys targaryen";
assertThat(testString, either(startsWith("tar")).or(containsString("targaryen")));

4. String Comparison

4.字符串比较

We can use containsString(String) or containsStringIgnoringCase(String) to assert if the actual string contains test string:

我们可以使用containsString(String)containsStringIgnoringCase(String)来断言实际字符串是否包含测试字符串。

String testString = "Rhaegar Targaryen";
assertThat(testString, containsString("aegar"));
assertThat(testString, containsStringIgnoringCase("AEGAR"));

Or startsWith(String) and startsWithIgnoringCase(String) to assert if the actual string starts with test string:

或者startsWith(String)startsWithIgnoringCase(String)来断言实际字符串是否以测试字符串开始。

assertThat(testString, startsWith("Rhae"));
assertThat(testString, startsWithIgnoringCase("rhae"));

We can also use endsWith(String) or endsWithIgnoringCase(String) to assert if the actual string ends with test string:

我们还可以使用endsWith(String)endsWithIgnoringCase(String)来断言实际字符串是否以测试字符串结束。

assertThat(testString, endsWith("aryen"));
assertThat(testString, endsWithIgnoringCase("ARYEN"));

5. Conclusion

5.结论

In this article, we discussed different methods of CoreMatchers class in Hamcrest library.

在这篇文章中,我们讨论了CoreMatchers类中Hamcrest库的不同方法。

And, as always, the source code for the examples can be found over on GitHub.

而且,像往常一样,这些例子的源代码可以在GitHub上找到