Hamcrest Text Matchers – 哈姆雷特文本匹配器

最后修改: 2018年 3月 16日

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

1. Overview

1.概述

In this tutorial, we’ll explore Hamcrest Text Matchers.

在本教程中,我们将探讨Hamcrest文本匹配器。

We discussed Hamcrest Matchers in general before in testing with Hamcrest, in this tutorial we’ll focus on Text Matchers only.

我们之前在使用Hamcrest进行测试中讨论了Hamcrest匹配器的总体情况,在本教程中我们将只关注Text匹配器。

2. Maven Configuration

2.Maven配置

First, we need to add the following dependency to our pom.xml:

首先,我们需要在我们的pom.xml中添加以下依赖关系。

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

The latest version of java-hamcrest can be downloaded from Maven Central.

最新版本的java-hamcrest可以从Maven中心下载。

Now, we’ll dive right into Hamcrest Text Matchers.

现在,我们将直接深入了解Hamcrest文本匹配器。

3. Text Equality Matchers

3.文本等价匹配器

We can, of course, check if two Strings are equal using the standard isEqual() matcher.

当然,我们可以使用标准的isEqual()匹配器检查两个字符串是否相等。

In addition, we have two matchers that are specific to String types: equalToIgnoringCase() and equalToIgnoringWhiteSpace().

此外,我们有两个专门针对String类型的匹配器。equalToIgnoringCase()equalToIgnoringWhiteSpace()

Let’s check if two Strings are equal – ignoring case:

让我们来检查两个字符串是否相等 – 忽略情况。

@Test
public void whenTwoStringsAreEqual_thenCorrect() {
    String first = "hello";
    String second = "Hello";

    assertThat(first, equalToIgnoringCase(second));
}

We can also check if two Strings are equal – ignoring leading and trailing whitespace:

我们还可以检查两个字符串是否相等 – 忽略前导和尾部的空白。

@Test
public void whenTwoStringsAreEqualWithWhiteSpace_thenCorrect() {
    String first = "hello";
    String second = "   Hello   ";

    assertThat(first, equalToIgnoringWhiteSpace(second));
}

4. Empty Text Matchers

4.空的文本匹配器

We can check if a String is blank, meaning it contains only whitespace, by using the blankString() and blankOrNullString() matchers:

我们可以通过使用blankString()blankOrNullString()匹配器来检查一个字符串是否为空白,即它只包含空格。

@Test
public void whenStringIsBlank_thenCorrect() {
    String first = "  ";
    String second = null;
    
    assertThat(first, blankString());
    assertThat(first, blankOrNullString());
    assertThat(second, blankOrNullString());
}

On the other hand, if we want to verify if a String is empty, we can use the emptyString() matchers:

另一方面,如果我们想验证一个字符串是否为空,我们可以使用emptyString()匹配器。

@Test
public void whenStringIsEmpty_thenCorrect() {
    String first = "";
    String second = null;

    assertThat(first, emptyString());
    assertThat(first, emptyOrNullString());
    assertThat(second, emptyOrNullString());
}

5. Pattern Matchers

5.模式匹配器

We can also check if a given text matches a regular expression using the matchesPattern() function:

我们还可以使用matchesPattern()函数检查一个给定的文本是否与正则表达式相匹配。

@Test
public void whenStringMatchPattern_thenCorrect() {
    String first = "hello";

    assertThat(first, matchesPattern("[a-z]+"));
}

6. Sub-String Matchers

6.子字符串匹配器

We can determine if a text contains another sub-text by using the containsString() function or containsStringIgnoringCase():

我们可以通过使用containsString()函数或containsStringIgnoringCase()来确定一个文本是否包含其他子文本:

@Test
public void whenVerifyStringContains_thenCorrect() {
    String first = "hello";

    assertThat(first, containsString("lo"));
    assertThat(first, containsStringIgnoringCase("EL"));
}

If we expect the sub-strings to be in a specific order, we can call the stringContainsInOrder() matcher:

如果我们希望子字符串以特定的顺序出现,我们可以调用stringContainsInOrder()匹配器。

@Test
public void whenVerifyStringContainsInOrder_thenCorrect() {
    String first = "hello";
    
    assertThat(first, stringContainsInOrder("e","l","o"));
}

Next, let’s see how to check that a String starts with a given String:

接下来,让我们看看如何检查一个String是否以给定的String开头。

@Test
public void whenVerifyStringStartsWith_thenCorrect() {
    String first = "hello";

    assertThat(first, startsWith("he"));
    assertThat(first, startsWithIgnoringCase("HEL"));
}

And finally, we can check if a String ends with a specified String:

最后,我们可以检查一个String是否以指定的String结束。

@Test
public void whenVerifyStringEndsWith_thenCorrect() {
    String first = "hello";

    assertThat(first, endsWith("lo"));
    assertThat(first, endsWithIgnoringCase("LO"));
}

7. Conclusion

7.结论

In this quick tutorial, we explored Hamcrest Text Matchers.

在这个快速教程中,我们探讨了Hamcrest文本匹配器。

As always, the full source code for the examples can be found over on GitHub.

一如既往,可以在GitHub上找到这些例子的完整源代码