Close

AssertJ - File and Path Assertions

[Last Updated: Mar 3, 2026]

When code generates files — export features, configuration writers, log processors — the tests need to verify not just that the file was created, but also that it exists, is readable, has the right content, or is a directory. AssertJ provides dedicated assertions for both java.io.File and java.nio.file.Path.

File vs Path Assertions

AssertJ supports both the older File API and the modern NIO2 Path API. The assertions available are similar between the two, including checks for existence, content, size, and permissions.

Key Methods

exists, doesNotExist, isFile, isDirectory, isReadable, hasContent, hasExtension, hasName, isEmptyFile.

Example

package com.logicbig.example;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.assertj.core.api.Assertions.*;

public class FilePathAssertionsExample {

    @Test
    void myTest() throws IOException {
        // create a temp file with content
        Path tempFile = Files.createTempFile("assertj-test-", ".txt");
        Files.writeString(tempFile, "hello assertj");

        File file = tempFile.toFile();

        // file existence and type
        assertThat(file).exists().isFile().isReadable();
        System.out.println("Existence/type checks passed");

        // file content
        assertThat(file).hasContent("hello assertj");
        System.out.println("Content check passed");

        // NIO Path
        assertThat(tempFile).exists().isReadable();
        System.out.println("Path assertion passed");

        // directory check
        File dir = tempFile.getParent().toFile();
        assertThat(dir).isDirectory();
        System.out.println("Directory check passed");

        // cleanup
        Files.deleteIfExists(tempFile);
    }
}

Output

$ mvn clean test -Dtest=*
[INFO] Scanning for projects...
[INFO]
[INFO] ---------< com.logicbig.example:assertj-file-path-assertions >----------
[INFO] Building assertj-file-path-assertions 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ assertj-file-path-assertions ---
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ assertj-file-path-assertions ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-file-path-assertions\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ assertj-file-path-assertions ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ assertj-file-path-assertions ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-file-path-assertions\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ assertj-file-path-assertions ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 1 source file with javac [debug target 17] to target\test-classes
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ assertj-file-path-assertions ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.logicbig.example.FilePathAssertionsExample
Existence/type checks passed
Content check passed
Path assertion passed
Directory check passed
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.290 s -- in com.logicbig.example.FilePathAssertionsExample
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.440 s
[INFO] Finished at: 2026-03-03T14:24:02+08:00
[INFO] ------------------------------------------------------------------------

Conclusion

The output confirms file creation, content, and directory checks all pass. AssertJ's file assertions eliminate the need for verbose assertTrue(file.exists()) calls, replacing them with expressive, chainable methods that also produce clear failure messages showing the full file path when a check fails.

Example Project

Dependencies and Technologies Used:

  • assertj-core 3.27.7 (Rich and fluent assertions for testing in Java)
  • junit-jupiter-engine 6.0.2 (Module "junit-jupiter-engine" of JUnit)
  • JDK 17
  • Maven 3.9.11

AssertJ - File and Path Assertions Select All Download
  • assertj-file-path-assertions
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • FilePathAssertionsExample.java

    See Also

    Join