junit-team/junit5 · error · PreconditionViolationException

The charset supplied in ${csvFileSource} is invalid

Error message

The charset supplied in ${csvFileSource} is invalid

What it means

CsvFileArgumentsProvider.getCharsetFrom calls Charset.forName with the @CsvFileSource.encoding attribute and wraps any failure as a PreconditionViolationException. The supplied encoding string is not a charset name or alias recognized by the JVM.

Source

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

		Stream<Source> resources = Arrays.stream(csvFileSource.resources()).map(inputStreamProvider::classpathResource);
		Stream<Source> files = Arrays.stream(csvFileSource.files()).map(inputStreamProvider::file);
		List<Source> sources = Stream.concat(resources, files).toList();

		// @formatter:off
		return Preconditions.notEmpty(sources, "Resources or files must not be empty")
				.stream()
				.map(source -> source.open(context))
				.map(inputStream -> CsvReaderFactory.createReaderFor(configuration, inputStream, charset))
				.flatMap(reader -> toStream(reader, csvFileSource));
		// @formatter:on
	}

	private static Charset getCharsetFrom(CsvFileSource csvFileSource) {
		try {
			return Charset.forName(csvFileSource.encoding());
		}
		catch (Exception ex) {
			throw new PreconditionViolationException("The charset supplied in " + csvFileSource + " is invalid", ex);
		}
	}

	private static Stream<Arguments> toStream(CsvReader<? extends CsvRecord> reader, CsvFileSource csvFileSource) {
		var spliterator = CsvExceptionHandlingSpliterator.delegatingTo(reader.spliterator(), csvFileSource);
		boolean useHeadersInDisplayName = csvFileSource.useHeadersInDisplayName();
		// @formatter:off
		return StreamSupport.stream(spliterator, false)
				.skip(csvFileSource.numLinesToSkip())
				.map(record -> CsvArgumentsProvider.processCsvRecord(
						record, useHeadersInDisplayName)
				)
				.onClose(() -> {
					try {
						reader.close();
					}
					catch (Throwable throwable) {
						throw CsvArgumentsProvider.handleCsvException(throwable, csvFileSource);

View on GitHub (pinned to 956246301e)

Solutions

  1. Use a canonical charset name (e.g. UTF-8, ISO-8859-1, US-ASCII).
  2. Omit the encoding attribute entirely to fall back to the default (UTF-8).
  3. Verify availability up front via Charset.availableCharsets().

Example fix

// before
@CsvFileSource(resources = "/data.csv", encoding = "UTF8-BOM")

// after
@CsvFileSource(resources = "/data.csv", encoding = "UTF-8")
Defensive patterns

Strategy: validation

Validate before calling

// Verify the encoding name resolves before using @CsvFileSource.
String encoding = "UTF-8";
try {
    Charset.forName(encoding);
} catch (IllegalArgumentException ex) {
    throw new IllegalArgumentException("Invalid @CsvFileSource encoding: " + encoding, ex);
}

Try / catch

try {
    // run the CSV-file parameterized test
} catch (PreconditionViolationException e) {
    if (e.getMessage().contains("charset supplied in")) {
        // switch to a canonical charset name (UTF-8) or drop the encoding attribute
    } else throw e;
}

Prevention

When it happens

Trigger: Setting @CsvFileSource(encoding = "...") to a value Charset.forName rejects - a typo, a non-registered alias, or an unsupported IANA name on a stripped JRE.

Common situations: Typo such as 'UTF-8 ' (trailing space) or 'UTS-8'; using an IANA name not aliased on the runtime; custom charset provider missing on a minimal JRE.

Related errors


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