junit-team/junit5 · error · PreconditionViolationException
Pattern compilation failed for a regular expression…
Error message
Pattern compilation failed for a regular expression supplied in %s
What it means
Thrown by EnumSource validator validatePatterns when one of the regular expressions supplied via @CsvSource(names = ...) or @EnumSource(names = ..., mode = MATCH_ANY/MATCH_ALL) cannot be compiled by Pattern.compile. The cause is the PatternSyntaxException describing the malformed regex; the message names the @EnumSource annotation.
Solutions
- Fix the regex syntax in @EnumSource(names = ...).
- If you meant literal names, use the default mode (no MATCH_ANY/MATCH_ALL) so names are matched literally, or escape regex metacharacters.
- Test the pattern in isolation with Pattern.compile before applying it.
Example fix
// before @EnumSource(value = TimeUnit.class, names = "[SECONDS", mode = Mode.MATCH_ANY) // after @EnumSource(value = TimeUnit.class, names = ".*SECOND.*", mode = Mode.MATCH_ANY)
Defensive patterns
Strategy: validation
Validate before calling
// Compile each name pattern before running the parameterized test
for (String name : names) {
try { Pattern.compile(name); }
catch (PatternSyntaxException e) { throw new IllegalArgumentException("Bad regex in @EnumSource names: " + name, e); }
} Type guard
static boolean isValidRegex(String pattern) {
try { Pattern.compile(pattern); return true; } catch (PatternSyntaxException e) { return false; }
} Prevention
- When using MATCH_ANY/MATCH_ALL, remember @EnumSource names are regexes.
- Test regex patterns in isolation first.
- For literal names, omit the matching mode so names are matched exactly.
When it happens
Trigger: Supplying an invalid regular expression in @EnumSource(names = ...) with a matching mode. names.forEach(Pattern::compile) throws PatternSyntaxException for a syntactically invalid pattern.
Common situations: Unclosed character class, unbalanced parentheses, invalid quantifier, or stray regex metacharacters in the names pattern. Forgetting that names are interpreted as regexes when a MATCH mode is used.
Related errors
- AnnotationBasedArgumentsProvider does not override the…
- ArgumentsProvider does not override the…
- Assumption failed
- Cannot convert to
- Configuration error: .
AI-assisted analysis of junit-team/junit5@f070c699a0 (2026-08-11).
Data as JSON: /api/errors/7e2d177ce9851202.
Report an issue: GitHub.
Appendix: source
Thrown at junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/EnumSource.java:228
Preconditions.notNull(constant, "Enum constant must not be null");
Preconditions.notNull(names, "names must not be null");
return selector.test(constant.name(), names);
}
private static void validateNames(EnumSource enumSource, Set<? extends Enum<?>> constants, Set<String> names) {
Set<String> allNames = constants.stream().map(Enum::name).collect(toSet());
Preconditions.condition(allNames.containsAll(names),
() -> "Invalid enum constant name(s) in " + enumSource + ". Valid names include: " + allNames);
}
private static void validatePatterns(EnumSource enumSource, Set<? extends Enum<?>> constants,
Set<String> names) {
try {
names.forEach(Pattern::compile);
}
catch (PatternSyntaxException e) {
throw new PreconditionViolationException(
"Pattern compilation failed for a regular expression supplied in " + enumSource, e);
}
}
private interface Validator {
void validate(EnumSource enumSource, Set<? extends Enum<?>> constants, Set<String> names);
}
}
}
View on GitHub (pinned to f070c699a0)