junit-team/junit5 · error · JUnitException

Failed to initialize AnnotationConsumer: %s

Error message

Failed to initialize AnnotationConsumer: %s

What it means

Thrown by AnnotationConsumerInitializer.initializeAnnotationConsumer when the consumer's accept(annotation) method raises any Exception during initialization. The framework wraps it in a JUnitException whose message is 'Failed to initialize AnnotationConsumer: <instance>' and chains the original exception as cause.

Source

Thrown at junit-jupiter-params/src/main/java/org/junit/jupiter/params/support/AnnotationConsumerInitializer.java:100

	@SuppressWarnings("unchecked")
	private static Class<? extends Annotation> getAnnotationType(Method method) {
		int annotationIndex = methodSignatures.stream() //
				.filter(signature -> signature.matches(method)) //
				.findFirst() //
				.map(MethodSignature::annotationParameterIndex) //
				.orElse(0);

		return (Class<? extends Annotation>) method.getParameterTypes()[annotationIndex];
	}

	private static <A extends Annotation> void initializeAnnotationConsumer(AnnotationConsumer<A> instance,
			A annotation) {
		try {
			instance.accept(annotation);
		}
		catch (Exception ex) {
			throw new JUnitException("Failed to initialize AnnotationConsumer: " + instance, ex);
		}
	}

	/**
	 * Annotation-consuming method signature.
	 */
	private record MethodSignature(String methodName, int parameterCount, int annotationParameterIndex) {

		boolean matches(Method method) {
			return method.getName().equals(methodName) //
					&& method.getParameterCount() == parameterCount //
					&& method.getParameterTypes()[annotationParameterIndex].isAnnotation();
		}
	}

}

View on GitHub (pinned to f070c699a0)

Solutions

  1. Inspect the wrapped cause exception for the real failure in accept().
  2. Fix the consumer's accept() implementation to handle the given annotation values.
  3. Correct the annotation attributes so they satisfy the consumer's expectations.

Example fix

// before
class MyProvider implements AnnotationConsumer<MyAnn> {
  public void accept(MyAnn a) { Objects.requireNonNull(a.value()); } // throws NPE if value() is null
}

// after
class MyProvider implements AnnotationConsumer<MyAnn> {
  public void accept(MyAnn a) {
    Preconditions.notBlank(a.value(), "value must be set on @MyAnn");
  }
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Validate annotation attributes before they reach accept()
String v = annotation.value();
if (v == null || v.isBlank())
    throw new IllegalArgumentException("@MyAnnotation value must not be blank");

Try / catch

try {
    AnnotationConsumerInitializer.initialize(element, provider);
} catch (JUnitException e) {
    if (e.getMessage().contains("Failed to initialize AnnotationConsumer")) {
        // inspect e.getCause() for the real failure in accept()
    }
    throw e;
}

Prevention

When it happens

Trigger: An AnnotationConsumer.accept(A) implementation throws (NullPointerException, IllegalArgumentException, custom error) while processing the annotation values. initializeAnnotationConsumer catches Exception and wraps it.

Common situations: Custom argument source whose accept() validates annotation attributes strictly and rejects invalid combinations. Bug in the consumer's accept() method. Annotation values out of expected range triggering an exception inside accept().

Related errors


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