Reputation: 679
I annotated some methods with a custom annotation: "ExistForTesting"
@Documented
@Target({TYPE, FIELD, METHOD})
@Retention(SOURCE)
public @interface ExistForTesting {
String why() default "";
}
I'd like to write a (Junit5 + ArchUnit) unit test that ensures ExistForTesting annotated methods are used from unit tests only. So, I wrote the given test:
ArchCondition<JavaClass> NOT_CALL_TEST_METHODS =
new ArchCondition<JavaClass>("not call methods annotated with ExistForTesting") {
@Override
public void check(@NotNull JavaClass item, ConditionEvents events) {
item.getMethodCallsFromSelf().stream().filter(method -> method.getTarget().isAnnotatedWith(ExistForTesting.class))
.forEach(method -> {
String message = String.format(
"Method %s is annotated with ExistForTesting",
method.getTarget().getFullName());
events.add(SimpleConditionEvent.violated(method, message));
});
}
};
@Test
public void shouldNotUseTestMethods() {
classes().that()
.haveSimpleNameNotEndingWith("Test")
.should(NOT_CALL_TEST_METHODS)
.check(appClasses);
}
Problem: my annotation is not detected.
The debugger finds methods calls, but method.getTarget().isAnnotatedWith(ExistForTesting.class))
always returns false
.
What am I doing wrong?
Upvotes: 1
Views: 1272
Reputation: 679
Ok, my annotation retention was set to SOURCE instead of CLASS.
Fixed.
Upvotes: 1