ArchUnit 测试 importOptions DoNotIncludeTests 不起作用 [英] ArchUnit test importOptions DoNotIncludeTests not working

查看:23
本文介绍了ArchUnit 测试 importOptions DoNotIncludeTests 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using ArchUnit 0.18 and archunit-junit5 from a Spring 2.3.8 application. For some reason I can't find, ImportOption.DoNotIncludeTests.class is not working as expected and test classes are also included. I can only get the ArchUnit test working by commenting the code in those tests classes.

ArchUnit 测试是:

ArchUnit test is:

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);
    }
}

我是否错过了一些步骤以便不参加测试课程?

Am I missing some step in order to not get into account test classes?

谢谢!

推荐答案

注解 @AnalyzeClasses(packages = "com.petproject", importOptions = { ImportOption.DoNotIncludeTests.class }) 不是在这种情况下进行评估.首先,测试方法使用JUnit 的@Test 注释,而不是ArchUnit 的@ArchTest.其次,根据

The annotation @AnalyzeClasses(packages = "com.petproject", importOptions = { ImportOption.DoNotIncludeTests.class }) is not evaluated in this case. First, the test method is annotated with JUnit's @Test instead of ArchUnit's @ArchTest. Second, the rule is checked against the result of

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

正如您已经想到的,您可以将其替换为

As you already figured out, you could replace this with

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

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

Alternatively you could change your test setup to

@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 = ...
}

这篇关于ArchUnit 测试 importOptions DoNotIncludeTests 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆