Java ArchUnit:在getMethodCallsFromSelf方法上检测注释

Java ArchUnit:在getMethodCallsFromSelf方法上检测注释,java,archunit,Java,Archunit,我用一个自定义注释对一些方法进行了注释:“ExistForTesting” 我想编写一个(Junit5+ArchUnit)单元测试,以确保只从单元测试中使用ExistForTesting带注释的方法。因此,我编写了给定的测试: ArchCondition<JavaClass> NOT_CALL_TEST_METHODS = new ArchCondition<JavaClass>("not call methods annotated with ExistForT

我用一个自定义注释对一些方法进行了注释:“ExistForTesting”

我想编写一个(Junit5+ArchUnit)单元测试,以确保只从单元测试中使用ExistForTesting带注释的方法。因此,我编写了给定的测试:

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);
}
ArchCondition不调用测试方法=
新的ArchCondition(“不调用用ExistForTesting注释的方法”){
@凌驾
公共无效检查(@NotNull JavaClass项,ConditionEvents){
item.getMethodCallsFromSelf().stream().filter(方法->方法.getTarget().isAnnotatedWith(ExistForTesting.class))
.forEach(方法->{
字符串消息=String.format(
“方法%s用ExistForTesting注释”,
方法getTarget().getFullName());
add(SimpleConditionEvent.invalized(方法,消息));
});
}
};
@试验
public void不应使用TestMethods(){
类()
.haveSimpleNameNotEndingWith(“测试”)
.should(不调用测试方法)
.检查(应用程序类);
}
问题:未检测到我的批注。
调试器查找方法调用,但
method.getTarget().isAnnotatedWith(ExistForTesting.class))
始终返回
false


我做错了什么?

好的,我的注释保留设置为源代码而不是类。

已修复。

您可以接受自己的答案,使其在概览中可见,即您的问题已解决。
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);
}