【问题标题】:Checking annotation presence in subclasses with ArchUnit使用 ArchUnit 检查子类中的注释存在
【发布时间】:2020-05-22 13:10:41
【问题描述】:

我正在使用 JUnit 5 评估 ArchUnit 0.13.1,并尝试编写如下规则:

    @ArchTest
    private final ArchRule annotationInheritance = ArchRuleDefinition.classes()
            .that().areAnnotatedWith(MyAnnotation.class)
            .should().onlyHaveSubclassesThat().areAnnotatedWith(MyAnnotation.class);

问题是 ArchUnit 中没有方法“onlyHaveSubclassesThat()”。 我确信可能还有另一种方法可以实现相同的目标,但是如何实现呢?

【问题讨论】:

    标签: java unit-testing inheritance annotations archunit


    【解决方案1】:

    您可以将检查应用于继承某个类的子类:

    classes()
        .that().areAssignableTo(CanBeAnnotated.Predicates.annotatedWith(MyAnnotation.class))
        .should().beAnnotatedWith(MyAnnotation.class);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      相关资源
      最近更新 更多