1

我正在尝试使用 Arch Unit 设置测试来测试我的命名约定,遵循官方示例

ArchUnit 的命名断言似乎区分大小写,这让我很困扰。

我想测试包中没有任何类domain.service包含单词service.

给定一个类domain.service.FileSystemService

  • 该测试通过:
 @ArchTest
  val domain_service_should_not_have_names_containing_service: ArchRule =
    noClasses()
      .that().resideInAPackage("..domain.service..")
      .should().haveSimpleNameContaining("service")
  • 此测试失败:
 @ArchTest
  val domain_service_should_not_have_names_containing_service: ArchRule =
    noClasses()
      .that().resideInAPackage("..domain.service..")
      .should().haveSimpleNameContaining("Service")

我错过了什么吗?有没有办法让 ArchUnit 的比较不区分大小写?

4

1 回答 1

0

如果您不想haveNameMatching按照评论中的建议使用,您也可以创建自己的ArchConditions.

  public static ArchCondition<JavaClass> containName(String namePart) {
      return new NameContains(namePart);
  }

  private static class NameContains extends ArchCondition<JavaClass> {
      private String namePart;

      NameContains(String namePart) {
          super("contain '" + namePart + "' in the name");
          this.namePart = namePart;
      }

      @Override
      public void check(JavaClass javaClass, ConditionEvents events) {
          boolean containsName = javaClass.getSimpleName().toLowerCase().contains(namePart.toLowerCase());
          String message;

          if (containsName) {
              message = createCheckMessage(javaClass, "contains '" + namePart + "' in the name");
          } else {
              message = createCheckMessage(javaClass, "does not contain '" + namePart + "' in the name");
          }

          events.add(new SimpleConditionEvent(javaClass, containsName, message));
      }
  }

  // taken from com.tngtech.archunit.lang.conditions.ArchConditions
  public static <T extends HasDescription & HasSourceCodeLocation> String createCheckMessage(T object,
          String message) {
      return object.getDescription() + " " + message + " in " + object.getSourceCodeLocation();
  }

你可以像这样使用它:

  rules.add(noClasses().that().resideInAPackage("..domain.service..").should(containName("Service")));
于 2020-08-25T07:31:20.530 回答