0

我正在使用 Spring 2.3.8 应用程序中的 ArchUnit 0.18 和 archunit-junit5。由于某种原因我找不到,ImportOption.DoNotIncludeTests.class没有按预期工作,并且还包括测试类。我只能通过在这些测试类中注释代码来让 ArchUnit 测试工作。

ArchUnit 测试是:

import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.core.importer.ImportOption;
import com.tngtech.archunit.junit.AnalyzeClasses;
import org.junit.jupiter.api.Test;

@AnalyzeClasses(packages = "com.petproject", importOptions = { ImportOption.DoNotIncludeTests.class })
class ArchitectureTest {

    @Test
    public void some_architecture_rule() {

        JavaClasses classes = new ClassFileImporter().importPackages("com.petproject");

        layeredArchitecture()
                .layer("Controller").definedBy("com.petproject.controller")
                .layer("Validators").definedBy("com.petproject.validations.validators")
                .layer("Service").definedBy("com.petproject.service")
                .layer("Persistence").definedBy("com.petproject.repository")

                .whereLayer("Controller").mayNotBeAccessedByAnyLayer()
                .whereLayer("Service").mayOnlyBeAccessedByLayers("Controller", "Validators")

                .check(classes);
    }
}

为了不考虑测试课程,我是否错过了一些步骤?

谢谢!

4

1 回答 1

1

@AnalyzeClasses(packages = "com.petproject", importOptions = { ImportOption.DoNotIncludeTests.class })在这种情况下,不会评估注释。首先,测试方法是用 JUnit 注释@Test而不是 ArchUnit 注释的@ArchTest。其次,根据以下结果检查规则

JavaClasses classes = new ClassFileImporter().importPackages("com.petproject");

正如您已经知道的那样,您可以将其替换为

JavaClasses classes = new ClassFileImporter().withImportOption(ImportOption.Predefined.DO_NOT_INCLUDE_TESTS).importPackages("com.petproject");

或者,您可以将测试设置更改为

@AnalyzeClasses(packages = "com.petproject", importOptions = { ImportOption.DoNotIncludeTests.class })
class ArchitectureTest {

    @ArchTest
    public static void some_architecture_rule(JavaClasses classes) {
        yourCreatedRule.check(classes);
    }

    // or as a field instead of a method

    @ArchTest
    public static ArchRule some_architecture_rule = ...
}
于 2021-05-27T05:53:05.703 回答