junit-team/junit5 · error · PreconditionViolationException
Pattern compilation failed for a regular expression supplied
Error message
Pattern compilation failed for a regular expression supplied in ${enumSource} What it means
EnumSource.Mode.validatePatterns runs Pattern.compile on every entry of names when mode is MATCH_ALL, MATCH_ANY, or MATCH_NONE, and wraps PatternSyntaxException as PreconditionViolationException. At least one of the supplied regular expressions is malformed.
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 956246301e)
Solutions
- Compile each pattern with Pattern.compile in a scratch test to find the offending entry.
- Use Pattern.quote(s) (or \Q...\E) when you want literal matching.
- Switch the mode to INCLUDE or EXCLUDE for exact name matching without regex semantics.
- Fix the regex syntax (close brackets/parentheses, escape metacharacters).
Example fix
// before
@EnumSource(mode = Mode.MATCH_ALL, names = { "[A-Z" })
// after
@EnumSource(mode = Mode.MATCH_ALL, names = { "[A-Z]+" }) Defensive patterns
Strategy: validation
Validate before calling
// Pre-compile every @EnumSource pattern when using MATCH_* modes.
String[] names = { "[A-Z]+" };
try {
for (String n : names) java.util.regex.Pattern.compile(n);
} catch (java.util.regex.PatternSyntaxException e) {
throw new IllegalArgumentException("Invalid @EnumSource pattern: " + e.getMessage(), e);
} Try / catch
try {
// run the enum-source parameterized test
} catch (PreconditionViolationException e) {
if (e.getMessage().contains("Pattern compilation failed")) {
// fix the regex or switch mode to INCLUDE/EXCLUDE for literal names
} else throw e;
} Prevention
- Compile patterns in a scratch test before declaring them in @EnumSource.
- Use Pattern.quote for literals you do not want interpreted as regex.
- Prefer Mode.INCLUDE/EXCLUDE unless regex matching is actually needed.
When it happens
Trigger: @EnumSource(mode = Mode.MATCH_ALL, names = {"..."}) with an invalid regex such as an unbalanced bracket, dangling quantifier, or illegal escape.
Common situations: Forgetting to escape regex metacharacters; reusing a literal include-list as patterns; mismatched parentheses copied from documentation.
Related errors
- %s cannot convert to type [%s]. Only target type [%s] is sup
- Failed to parse CSV input configured via ${annotation}
- @EmptySource cannot provide an empty argument %s: [%s] is no
- AnnotationBasedArgumentsProvider does not override the provi
- Please implement provideArguments(ParameterDeclarations, Ext
AI-assisted analysis of junit-team/junit5@956246301e (2026-08-04).
Data as JSON: /data/errors/9c4c57888d8a6188.json.
Report an issue: GitHub.