使用 JUnit5 运行 Archunit 测试时未找到测试

问题描述 投票:0回答:1

我有一个非常简单的 Archunit 测试用例,在 IntelliJ IDE 中使用 Java 17 和 JUnit 5 运行。我在

pom.xml
中添加了以下依赖项:

            <dependency>
                <groupId>com.tngtech.archunit</groupId>
                <artifactId>archunit-junit5-api</artifactId>
                <version>1.1.0</version>
                <scope>test</scope>
            </dependency>

以下是我的测试课:

    import com.tngtech.archunit.junit.*;
    import com.tngtech.archunit.lang.ArchRule;
    import org.springframework.stereotype.Controller;
    
    import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.*;
    import static com.tngtech.archunit.core.importer.ImportOption.*;

        @AnalyzeClasses(
            packages = "com.my.package.name",
            importOptions = {DoNotIncludeTests.class})
        public class ArchitectureTests {
        
          @ArchTest
          public static final ArchRule testControllerNaming =
              classes()
                  .that()
                  .areAnnotatedWith(Controller.class)
                  .should()
                  .haveSimpleNameEndingWith("Controller");
        }

当我运行测试用例时,我不断收到“未找到测试”的消息。我错过了什么?

编辑 1:我使用 JUnit 注释得到了其中的一部分。下面的代码有效:

import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
import com.tngtech.archunit.lang.ArchRule;
import org.junit.Test;
import org.springframework.stereotype.Controller;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.*;

public class ArchitectureTests {
  JavaClasses jc = new ClassFileImporter().importPackages("com.my.package.name");

  public static final ArchRule testControllerNaming =
      classes()
          .that()
          .areAnnotatedWith(Controller.class)
          .should()
          .haveSimpleNameEndingWith("Controller")
          .allowEmptyShould(true);

  @Test
  public void testControllerName() {
    testControllerNaming.check(jc);
  }
}

但是,这仅在直接从 IDE 运行时有效。 Maven 似乎没有找到这些测试。 此外,

@ArchTest
注释似乎不起作用。我只能运行带有
@Test
注释的基于方法的测试。我无法运行任何直接用
@ArchTest
注释的基于现场的测试。 我将我的代码与 ArchUnit 官方 Github 存储库中列出的示例进行了比较,我似乎找不到任何可能导致我的代码无法工作的原因的差异。我将不胜感激任何指点。

spring-boot unit-testing java-8 junit5 archunit
1个回答
0
投票

正如@Manfred 暗示的那样,问题出在我的

pom.xml
。当我第一次没有得到预期的结果时,我开始添加一堆可能不需要的依赖项,并且它们可能相互冲突。我添加了以下依赖项:

  1. 基础单元
  2. archunit-junit5
  3. archunit-junit5-api
  4. archunit-junit5-引擎
  5. junit-platform-engine(当测试用例未被maven识别时显式添加)

我删除了所有内容并保留了

archunit-junit5=1.1.0
。刚刚成功了。下面是工作测试用例,在直接从 IDE 或 Maven 运行时都可以工作 - 正如预期的那样。

import com.tngtech.archunit.junit.AnalyzeClasses;
import com.tngtech.archunit.junit.ArchTest;
import com.tngtech.archunit.lang.ArchRule;
import org.springframework.stereotype.Controller;

import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.*;
import static com.tngtech.archunit.core.importer.ImportOption.*;

@AnalyzeClasses(
    packages = "com.my.package.name",
    importOptions = {DoNotIncludeTests.class})
public class ArchitectureTests {
  @ArchTest
  public static final ArchRule testControllerNaming =
      classes()
          .that()
          .areAnnotatedWith(Controller.class)
          .should()
          .haveSimpleNameEndingWith("Controller");
}
© www.soinside.com 2019 - 2024. All rights reserved.