Hamcrest File Matchers – Hamcrest文件匹配器

最后修改: 2018年 3月 16日

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

1. Overview

1.概述

In this tutorial, we’ll discuss Hamcrest File Matchers.

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

We discussed Hamcrest Matchers in general before in the previous Testing with Hamcrest article. In the next sections, we’ll focus only File Matchers.

我们在之前的使用Hamcrest测试文章中对Hamcrest匹配器进行了一般性的讨论。在接下来的章节中,我们将只关注文件匹配器。

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中心下载。

Let’s continue with exploring the Hamcrest File Matchers.

让我们继续探索Hamcrest的文件匹配器。

3. File Properties

3.文件属性

Hamcrest provides several matchers that verify commonly used File properties.

Hamcrest提供了几个匹配器来验证常用的文件属性。

Let’s see how we can verify the File name using aFileNamed() combined with a String Matcher:

让我们看看我们如何使用aFileNamed()结合String匹配器来验证文件的名称。

@Test
public void whenVerifyingFileName_thenCorrect() {
    File file = new File("src/test/resources/test1.in");
 
    assertThat(file, aFileNamed(equalToIgnoringCase("test1.in")));
}

We can also assess the file path – again in combination with a String Matcher:

我们还可以评估文件路径–再次与String匹配器结合使用。

@Test
public void whenVerifyingFilePath_thenCorrect() {
    File file = new File("src/test/resources/test1.in");
    
    assertThat(file, aFileWithCanonicalPath(containsString("src/test/resources")));
    assertThat(file, aFileWithAbsolutePath(containsString("src/test/resources")));
}

Let’s also see a file’s size – in bytes:

我们也来看看一个文件的大小–以字节为单位。

@Test
public void whenVerifyingFileSize_thenCorrect() {
    File file = new File("src/test/resources/test1.in");

    assertThat(file, aFileWithSize(11));
    assertThat(file, aFileWithSize(greaterThan(1L)));;
}

Finally, we can check if a File is readable and writable:

最后,我们可以检查一个文件是否可读和可写。

@Test
public void whenVerifyingFileIsReadableAndWritable_thenCorrect() {
    File file = new File("src/test/resources/test1.in");

    assertThat(file, aReadableFile());
    assertThat(file, aWritableFile());        
}

4. Existing File Matcher

4.现有的文件匹配器

If we want to verify that a File or directory exists, we can use the anExistingFile() or anExistingDirectory() matchers:

如果我们想验证一个文件或目录是否存在,我们可以使用anExistingFile()anExistingDirectory()匹配器。

@Test
public void whenVerifyingFileOrDirExist_thenCorrect() {
    File file = new File("src/test/resources/test1.in");
    File dir = new File("src/test/resources");
    
    assertThat(file, anExistingFile());
    assertThat(dir, anExistingDirectory());
    assertThat(file, anExistingFileOrDirectory());
    assertThat(dir, anExistingFileOrDirectory());
}

The anExistingFileOrDirectory() matcher that combines the two is also available.

将两者结合起来的anExistingFileOrDirectory()匹配器也可用。

5. Conclusion

5.结论

In this quick article, we went through Hamcrest File Matchers and their use.

在这篇快速文章中,我们了解了Hamcrest文件匹配器及其使用。

As always, the full source code for the examples is available over on GitHub.

一如既往,这些示例的完整源代码可在GitHub上获得over