【问题标题】:How do I create an ArchUnit rule to verify all exceptions thrown inherit from a specific custom exception?如何创建 ArchUnit 规则来验证抛出的所有异常都继承自特定的自定义异常?
【发布时间】:2020-08-20 13:52:19
【问题描述】:

我的架构规则之一是应用程序代码抛出的所有异常必须是 CustomException 或 CustomException 的子类。

我在 ArchUnit 中编写这条规则时遇到了困难。我目前拥有的是以下内容:

private static final ArchCondition<JavaClass> THROWS_NON_CUSTOM_EXCEPTION = callCodeUnitWhere( 
   target(is(constructor()))
      .and(
         originOwner(
            is(assignableTo(Throwable.class)) 
            .and(not(assignableTo(CustomException.class)))
         )
      )
   );

@ArchTest public static final ArchRule noNonCustomExceptions = noClasses()
   .should(THROWS_NON_CUSTOM_EXCEPTION);

运行此规则会返回 true(JUnit 测试通过),即使我的代码会引发不是 CustomException 的异常。

我已经测试了检测构造函数具有可分配给 Throwable 的所有者的规则部分:

private static final ArchCondition<JavaClass> THROWS_NON_CUSTOM_EXCEPTION =    
   callCodeUnitWhere( 
      target(is(constructor()))
         .and(
            originOwner( 
               is(assignableTo(Throwable.class))
            )
         )
   );

这会正确返回代码中创建任何 Throwable 的每个位置。

问题似乎出在我的代码中,它试图找到不可分配给 CustomException 的所有者:

   private static final ArchCondition<JavaClass> THROWS_NON_CUSTOM_EXCEPTION = callCodeUnitWhere( 
      target(is(constructor()))
         .and(
            originOwner( 
               is(not(assignableTo(CustomException.class)))
            )
         )
   );

这将返回每个构造函数,即使是那些可分配给 CustomException 的异常。

在 ArchUnit 中编写规则以返回调用构造函数的每个代码单元的正确方法是什么,该构造函数的所有者可分配给 Throwable,并且不可分配给 CustomException?

【问题讨论】:

    标签: java unit-testing archunit


    【解决方案1】:

    我认为您只需将已有的部分组合起来。以下代码适用于我:

    noClasses().should().callCodeUnitWhere(
        JavaCall.Predicates.target(
            AccessTarget.Predicates.constructor()
            .and(AccessTarget.Predicates.declaredIn(JavaClass.Predicates.assignableTo(Throwable.class)))
            .and(DescribedPredicate.not(AccessTarget.Predicates.declaredIn(JavaClass.Predicates.assignableTo(BaseException.class))))
        )
    );
    

    如果您的 BaseException 位于分析范围内,则需要排除该类:noClasses().that(not(belongToAnyOf(BaseException.class)))。

    【讨论】:

      猜你喜欢
      • 2018-02-14
      • 2011-10-06
      • 2015-10-14
      • 1970-01-01
      • 2014-02-22
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多