junit-team/junit5 · error · JUnitException

File [${path}] could not be read

Error message

File [${path}] could not be read

What it means

DefaultInputStreamProvider.openFile opens the path via Files.newInputStream(Path.of(path)) and wraps IOException in a JUnitException. A path listed in the @CsvFileSource.files array does not exist, is not a regular file, or is not readable.

Source

Thrown at junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/CsvFileArgumentsProvider.java:178

		private static final DefaultInputStreamProvider INSTANCE = new DefaultInputStreamProvider();

		@Override
		public InputStream openClasspathResource(Class<?> baseClass, String path) {
			Preconditions.notBlank(path, () -> "Classpath resource [" + path + "] must not be null or blank");
			//noinspection resource (closed elsewhere)
			InputStream inputStream = baseClass.getResourceAsStream(path);
			return Preconditions.notNull(inputStream, () -> "Classpath resource [" + path + "] does not exist");
		}

		@Override
		public InputStream openFile(String path) {
			Preconditions.notBlank(path, () -> "File [" + path + "] must not be null or blank");
			try {
				return Files.newInputStream(Path.of(path));
			}
			catch (IOException e) {
				throw new JUnitException("File [" + path + "] could not be read", e);
			}
		}

	}

}

View on GitHub (pinned to 956246301e)

Solutions

  1. Put the file on the classpath and use the resources attribute instead of files.
  2. Use an absolute path or confirm the relative path against System.getProperty("user.dir").
  3. Verify with Files.isReadable(Path.of(path)) before running the suite.
  4. Check read permissions on the target file.

Example fix

// before (cwd-dependent, fragile)
@CsvFileSource(files = { "data.csv" })

// after (classpath, portable)
@CsvFileSource(resources = { "/com/acme/data.csv" })
Defensive patterns

Strategy: validation

Validate before calling

// Confirm the file exists and is readable before the suite starts.
java.nio.file.Path p = java.nio.file.Path.of("data.csv");
if (!java.nio.file.Files.isReadable(p)) {
    throw new IllegalStateException("CSV file not readable: " + p.toAbsolutePath());
}

Try / catch

try {
    // run the CSV-file parameterized test
} catch (JUnitException e) {
    if (e.getMessage().contains("could not be read")) {
        // move the file to the classpath and switch files -> resources, or fix the path
    } else throw e;
}

Prevention

When it happens

Trigger: @CsvFileSource(files = {"..."}) where the path cannot be resolved relative to the JVM working directory, the file was deleted, or the process lacks read permission.

Common situations: Relative path resolved against an unexpected cwd (IDE vs CI build); typo in the path; confusing files (filesystem) with resources (classpath); permission denied.

Related errors


AI-assisted analysis of junit-team/junit5@956246301e (2026-08-04). Data as JSON: /data/errors/03925fce1032b2f0.json. Report an issue: GitHub.