junit-team/junit5 · error · PreconditionViolationException

The charset supplied in %s is invalid

Error message

The charset supplied in %s is invalid

What it means

Thrown by CsvFileArgumentsProvider.getCharsetFrom when Charset.forName rejects the encoding name supplied in @CsvFileSource(encoding = ...). The message includes the @CsvFileSource annotation so the offending config is identifiable. The cause is the underlying UnsupportedCharsetException or IllegalCharsetNameException.

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 f070c699a0)

Solutions

  1. Use a canonical charset name such as UTF-8, ISO-8859-1, US-ASCII.
  2. Remove the encoding attribute to use the default charset if the file is in the platform default.
  3. Verify the charset is available: Charset.availableCharsets().containsKey(name).

Example fix

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

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

Strategy: validation

Validate before calling

// Validate the charset name before the test runs
String encoding = "UTF-8";
if (!Charset.availableCharsets().containsKey(encoding))
    throw new IllegalArgumentException("Unsupported charset: " + encoding);

Type guard

static boolean isValidCharset(String name) {
    try { Charset.forName(name); return true; } catch (Exception e) { return false; }
}

Prevention

When it happens

Trigger: Setting @CsvFileSource(encoding = "XYZ") where XYZ is not a valid/registered charset name in the JVM. Typo in a charset name, or referencing a charset not available in the runtime.

Common situations: Misspelled charset such as "UTF8" instead of "UTF-8", or "utf-8" with stray characters. Running on a JVM without the requested charset provider. Copy-paste from a system that used a non-IANA alias.

Related errors


AI-assisted analysis of junit-team/junit5@f070c699a0 (2026-08-11). Data as JSON: /api/errors/45b7c96d6a183efb. Report an issue: GitHub.