{"record":{"id":"82d08bfeee610d6f","repo":"junit-team/junit5","slug":"failed-to-initialize-annotationconsumer-s","errorCode":null,"errorMessage":"Failed to initialize AnnotationConsumer: %s","messagePattern":"Failed to initialize AnnotationConsumer: (.+?)","errorType":"exception","errorClass":"JUnitException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-params/src/main/java/org/junit/jupiter/params/support/AnnotationConsumerInitializer.java","lineNumber":100,"sourceCode":"\n\t@SuppressWarnings(\"unchecked\")\n\tprivate static Class<? extends Annotation> getAnnotationType(Method method) {\n\t\tint annotationIndex = methodSignatures.stream() //\n\t\t\t\t.filter(signature -> signature.matches(method)) //\n\t\t\t\t.findFirst() //\n\t\t\t\t.map(MethodSignature::annotationParameterIndex) //\n\t\t\t\t.orElse(0);\n\n\t\treturn (Class<? extends Annotation>) method.getParameterTypes()[annotationIndex];\n\t}\n\n\tprivate static <A extends Annotation> void initializeAnnotationConsumer(AnnotationConsumer<A> instance,\n\t\t\tA annotation) {\n\t\ttry {\n\t\t\tinstance.accept(annotation);\n\t\t}\n\t\tcatch (Exception ex) {\n\t\t\tthrow new JUnitException(\"Failed to initialize AnnotationConsumer: \" + instance, ex);\n\t\t}\n\t}\n\n\t/**\n\t * Annotation-consuming method signature.\n\t */\n\tprivate record MethodSignature(String methodName, int parameterCount, int annotationParameterIndex) {\n\n\t\tboolean matches(Method method) {\n\t\t\treturn method.getName().equals(methodName) //\n\t\t\t\t\t&& method.getParameterCount() == parameterCount //\n\t\t\t\t\t&& method.getParameterTypes()[annotationParameterIndex].isAnnotation();\n\t\t}\n\t}\n\n}\n","sourceCodeStart":82,"sourceCodeEnd":117,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-jupiter-params/src/main/java/org/junit/jupiter/params/support/AnnotationConsumerInitializer.java#L82-L117","documentation":"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.","triggerScenarios":"An AnnotationConsumer.accept(A) implementation throws (NullPointerException, IllegalArgumentException, custom error) while processing the annotation values. initializeAnnotationConsumer catches Exception and wraps it.","commonSituations":"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().","solutions":["Inspect the wrapped cause exception for the real failure in accept().","Fix the consumer's accept() implementation to handle the given annotation values.","Correct the annotation attributes so they satisfy the consumer's expectations."],"exampleFix":"// before\nclass MyProvider implements AnnotationConsumer<MyAnn> {\n  public void accept(MyAnn a) { Objects.requireNonNull(a.value()); } // throws NPE if value() is null\n}\n\n// after\nclass MyProvider implements AnnotationConsumer<MyAnn> {\n  public void accept(MyAnn a) {\n    Preconditions.notBlank(a.value(), \"value must be set on @MyAnn\");\n  }\n}","handlingStrategy":"try-catch","validationCode":"// Validate annotation attributes before they reach accept()\nString v = annotation.value();\nif (v == null || v.isBlank())\n    throw new IllegalArgumentException(\"@MyAnnotation value must not be blank\");","typeGuard":null,"tryCatchPattern":"try {\n    AnnotationConsumerInitializer.initialize(element, provider);\n} catch (JUnitException e) {\n    if (e.getMessage().contains(\"Failed to initialize AnnotationConsumer\")) {\n        // inspect e.getCause() for the real failure in accept()\n    }\n    throw e;\n}","preventionTips":["Validate annotation attributes defensively at the start of accept().","Throw PreconditionViolationException from accept() with actionable messages.","Unit-test accept() with representative annotation values."],"tags":["junit-jupiter","annotation-consumer","initialization","wrapping"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}