【问题标题】:Maven silently fails to find JUnit tests to runMaven 默默地找不到要运行的 JUnit 测试
【发布时间】:2019-12-20 01:22:37
【问题描述】:

我在 IntelliJ 中打开了一个以 Maven 作为构建工具的新 Java 项目,目前有一个类和一个 JUnit 5 测试类。当我指示 IntelliJ 单独或一起运行测试时,它可以工作。但是当我去终端并点击 mvn clean test 或从 IntelliJ 中的 Maven 窗格执行相同操作时,它会跳过测试。

但是,与the questioner with this similar question 不同,我没有收到任何错误消息。找到测试类 并且确实 编译。我没有他遇到的相同问题(文件命名不正确)。

编辑:Stackoverflow 询问我为什么这不是 this question 的副本。 同样的问题,但他们的解决方案(从 2016 年开始)不再正确。您不再需要添加“提供者”依赖项。

这是我的 Maven 输出的相关部分:

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ markovmodels ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\Users\joe\foo\markovmodels\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ markovmodels ---
[INFO] Surefire report directory: C:\Users\joe\foo\markovmodels\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.648 s
[INFO] Finished at: 2019-08-13T09:02:53-04:00
[INFO] ------------------------------------------------------------------------

我不知道这是否是一个有用的线索,但我观察到 target/surefire-reports 目录没有创建。

pom.xml我有这两个测试相关的依赖:

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.4.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.2</version>
        <scope>test</scope>
    </dependency>

它们是直接从另一个有效的项目复制过来的。我没有指定 Surefire 插件的版本或更改其任何默认值,因此有效的 POM 与我的其他项目相同(它使用 maven-surefire-plugin 版本 2.12.4)。测试源文件似乎位于正确的目录中并且具有正确的命名约定。 我会犯什么错误?

当前状态的代码可以是here on Github

【问题讨论】:

  • 使用最低限度的 maven-surefire-plugin 2.22.2 来正确使用 JUnit Jupiter...
  • 它适用于 junit 4.12 吗?您的测试课程是否以 Test 结尾?
  • 它不适用于junit 4,因为我使用了一些新方法,例如assertThrows。测试类以包含测试的@Nested 类结束。
  • Lesiak 和 khmarbaise 你们都为我指明了正确的方向。我需要指定 maven-surefire-plugin 的版本

标签: maven intellij-idea junit5 maven-surefire-plugin


【解决方案1】:

maven-surefire-plugin 有问题

maven-surefire-plugin 的默认版本有问题,我可以通过升级来修复它。我通过从JUnit5 sample Maven project on Github复制相关部分解决了这个问题:

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.5.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
        </plugin>
    </plugins>
</build>

【讨论】:

  • 在将spring-boot-starter-parent 声明为&lt;parent&gt; 时,对junit-jupitermaven-surefire-plugin 使用相同的声明仍然不起作用
  • @manasouza 也尝试添加 spring-boot-starter-test 依赖项。我在我的一个 Spring Boot 项目中有上述两个声明(加上其他一些 Spring Boot 启动器,包括 spring-boot-starter-test)并且它有效。无论如何,就目前而言。
  • 这是项目,如果你想尝试复制它:github.com/joeclark-phd/granite
猜你喜欢
  • 2011-09-04
  • 2011-05-09
  • 2013-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多