Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

1. Overview

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

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

2. Maven Configuration

First, we need to add the following dependency to our 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.

Let's continue with exploring the Hamcrest File Matchers.

3. File Properties

Hamcrest provides several matchers that verify commonly used File properties.

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

@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:

@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

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

@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.

5. Conclusion

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

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

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until the end of this week:

>> GET ACCESS NOW

Junit footer banner
Comments are closed on this article!