This documentation explains how to configure ApprovalTests.Java to locate your source files when tests run from a non-standard working directory.
ApprovalTests.Java needs to locate your test source files to determine where to place approval files. By default, it searches from the current working directory. However, in some scenarios, the working directory is not the project root:
- Maven Surefire with forked test execution: When using
<workingDirectory>target/fork_dir_${surefire.forkNumber}</workingDirectory>for parallel or isolated test execution - CI systems: When build tools execute tests from the
targetdirectory or other subdirectories
When the working directory differs from the project root, ApprovalTests may fail with an error like:
"Didn't find [TestClass] under .../target/fork_dir_2"
ApprovalTests provides a configuration option to explicitly specify the project root directory. You can set APPROVALTESTS_PROJECT_DIRECTORY as either an environment variable or a system property.
Set the environment variable to point to your project root:
Unix/Linux/macOS:
export APPROVALTESTS_PROJECT_DIRECTORY=/path/to/your/project
mvn testWindows:
set APPROVALTESTS_PROJECT_DIRECTORY=C:\path\to\your\project
mvn testWindows PowerShell:
$env:APPROVALTESTS_PROJECT_DIRECTORY="C:\path\to\your\project"
mvn testAlternatively, pass it as a Java system property:
mvn test -DAPPROVALTESTS_PROJECT_DIRECTORY=/path/to/your/projectIf you're using Maven Surefire with forked test execution, configure the system property in your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<forkCount>2</forkCount>
<parallel>methods</parallel>
<workingDirectory>target/fork_dir_${surefire.forkNumber}</workingDirectory>
<systemPropertyVariables>
<APPROVALTESTS_PROJECT_DIRECTORY>${project.basedir}</APPROVALTESTS_PROJECT_DIRECTORY>
</systemPropertyVariables>
</configuration>
</plugin>If your CI system runs tests from the target directory, set the environment variable to the parent directory:
Relative path example:
export APPROVALTESTS_PROJECT_DIRECTORY=..
mvn testAbsolute path example (GitHub Actions):
- name: Run tests
env:
APPROVALTESTS_PROJECT_DIRECTORY: ${{ github.workspace }}
run: mvn testAbsolute path example (Jenkins):
environment {
APPROVALTESTS_PROJECT_DIRECTORY = "${WORKSPACE}"
}