{"id":"8b0b35a30ea3442d","repo":"junit-team/junit5","slug":"failed-to-initialize-annotationconsumer-instanc","errorCode":null,"errorMessage":"Failed to initialize AnnotationConsumer: ${instance}","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/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-params/src/main/java/org/junit/jupiter/params/support/AnnotationConsumerInitializer.java#L82-L117","documentation":"AnnotationConsumerInitializer.initializeAnnotationConsumer calls instance.accept(annotation) inside a try and wraps any Exception as JUnitException. The custom AnnotationConsumer's accept method threw while validating or storing the annotation - typically because an annotation attribute failed a precondition.","triggerScenarios":"A custom provider's accept(A) method throws (e.g. a Preconditions.check on an attribute), surfacing during extension initialization before any test runs.","commonSituations":"Custom annotation with an invalid attribute (blank string, negative number, malformed value); NPE inside accept; third-party extension that validates its annotation eagerly.","solutions":["Read the cause of the JUnitException - it carries the real validation message from accept.","Fix the annotation attributes to satisfy the provider's accept contract.","If you author the provider, throw a precise exception (PreconditionViolationException) with an actionable message."],"exampleFix":"// before\n@MyAnno(value = \"\")   // provider's accept rejects blank value\n\n// after\n@MyAnno(value = \"valid\")","handlingStrategy":"try-catch","validationCode":"// Validate annotation attributes the way the provider's accept() will, before registration.\nString value = annotation.value();\nif (value == null || value.isBlank()) {\n    throw new IllegalArgumentException(\"@MyAnno.value must not be blank\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    // run the parameterized test that initializes the provider\n} catch (JUnitException e) {\n    if (e.getMessage().contains(\"Failed to initialize AnnotationConsumer\")) {\n        Throwable cause = e.getCause();\n        // read cause's message and fix the offending annotation attribute\n    } else throw e;\n}","preventionTips":["Inspect the wrapped cause - it carries the real validation message from accept().","Validate annotation attributes at declaration time in a build-time check.","As a provider author, throw PreconditionViolationException with an actionable message."],"tags":["junit","jupiter-params","annotation-consumer","validation"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}