{"record":{"id":"6fc2eb99476f5353","repo":"junit-team/junit5","slug":"s-must-be-used-with-an-annotation-of-type-s","errorCode":null,"errorMessage":"%s must be used with an annotation of type %s","messagePattern":"(.+?) must be used with an annotation of type (.+?)","errorType":"exception","errorClass":"JUnitException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-params/src/main/java/org/junit/jupiter/params/support/AnnotationConsumerInitializer.java","lineNumber":61,"sourceCode":"\t\tnew MethodSignature(\"provideArguments\", 2, 1), //\n\t\tnew MethodSignature(\"convert\", 3, 2));\n\n\tprivate static final Predicate<Method> consumesAnnotation = methodSignatures.stream() //\n\t\t\t.map(signature -> (Predicate<Method>) signature::matches) //\n\t\t\t.reduce(method -> false, Predicate::or);\n\n\tprivate AnnotationConsumerInitializer() {\n\t\t/* no-op */\n\t}\n\n\t@SuppressWarnings({ \"unchecked\", \"rawtypes\" })\n\tpublic static <T> T initialize(AnnotatedElement annotatedElement, T annotationConsumerInstance) {\n\t\tif (annotationConsumerInstance instanceof AnnotationConsumer consumer) {\n\t\t\tClass<? extends Annotation> annotationType = findConsumedAnnotationType(annotationConsumerInstance);\n\t\t\tList<? extends Annotation> annotations = findAnnotations(annotatedElement, annotationType);\n\n\t\t\tif (annotations.isEmpty()) {\n\t\t\t\tthrow new JUnitException(annotationConsumerInstance.getClass().getName()\n\t\t\t\t\t\t+ \" must be used with an annotation of type \" + annotationType.getName());\n\t\t\t}\n\n\t\t\tannotations.forEach(annotation -> initializeAnnotationConsumer(consumer, annotation));\n\t\t}\n\t\treturn annotationConsumerInstance;\n\t}\n\n\tprivate static <T extends Annotation> List<T> findAnnotations(AnnotatedElement annotatedElement,\n\t\t\tClass<T> annotationType) {\n\n\t\treturn annotationType.isAnnotationPresent(Repeatable.class)\n\t\t\t\t? findRepeatableAnnotations(annotatedElement, annotationType)\n\t\t\t\t: findAnnotation(annotatedElement, annotationType).map(Collections::singletonList).orElse(emptyList());\n\t}\n\n\tprivate static <T> Class<? extends Annotation> findConsumedAnnotationType(T annotationConsumerInstance) {\n\t\tMethod method = findMethods(annotationConsumerInstance.getClass(), consumesAnnotation, BOTTOM_UP).get(0);","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-jupiter-params/src/main/java/org/junit/jupiter/params/support/AnnotationConsumerInitializer.java#L43-L79","documentation":"Thrown by AnnotationConsumerInitializer.initialize when an AnnotationConsumer instance declares a consumed annotation type but the annotated element (field/method/class) carries none of those annotations. The initializer cannot bind the consumer without the annotation, so it raises a JUnitException naming the consumer class and the expected annotation type.","triggerScenarios":"Registering an AnnotationConsumer-based argument source/converter that expects, say, @MyAnnotation, but the test element is not annotated with @MyAnnotation. findAnnotations returns an empty list and the initializer throws.","commonSituations":"Custom argument provider/converter that consumes an annotation, used without that annotation present. Removing or renaming the annotation on the test element while the consumer still expects it. Wrong annotation import causing the type check to fail.","solutions":["Annotate the test element with the annotation type the consumer expects (as printed in the message).","If the consumer is the wrong one, replace the @ArgumentsSource/@ConvertWith reference with the correct class.","Ensure the annotation import matches exactly the type declared by the consumer."],"exampleFix":"// before\n@ParameterizedTest\n@ArgumentsSource(MyProvider.class)\nvoid test(String x) { }  // MyProvider expects @MyAnnotation\n\n// after\n@ParameterizedTest\n@MyAnnotation\n@ArgumentsSource(MyProvider.class)\nvoid test(String x) { }","handlingStrategy":"validation","validationCode":"// Confirm the test element carries the annotation the consumer expects\nClass<? extends Annotation> expected = MyAnnotation.class;\nif (!annotatedElement.isAnnotationPresent(expected))\n    throw new IllegalStateException(\"Missing required annotation \" + expected.getName());","typeGuard":"static boolean hasRequiredAnnotation(AnnotatedElement el, Class<? extends Annotation> ann) {\n    return el != null && el.isAnnotationPresent(ann);\n}","tryCatchPattern":null,"preventionTips":["Always pair a custom AnnotationConsumer with the annotation it expects on the test element.","Keep annotation imports consistent across consumer and usage.","Add a quick smoke test for each custom source/converter pair."],"tags":["junit-jupiter","annotation-consumer","initialization","configuration"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}