首页 > 解决方案 > 即使junit5测试失败,Maven也会返回exit 0

问题描述

我已经设置 Maven 来触发一些 junit5 测试。

当我运行时:

mvn clean integration-test

我看到项目构建,然后它也运行测试。我在其中一个单元测试中看到了失败。

在控制台输出的下方,我看到:

[INFO] BUILD SUCCESS

我究竟做错了什么?我的期望是失败的测试运行会停止像 maven 这样的工具并返回退出代码 1。

这在 CI 中尤其令人沮丧,因为它显示为测试是健康的,因为退出代码返回零。

编辑:这是 POM XML:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.github.(removedforstackoverflowpost)</groupId>
    <artifactId>(removedforstackoverflowpost)</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1</version>
    <name>(removedforstackoverflowpost)</name>
    <url>(removedforstackoverflowpost)</url>
    <description>(removedforstackoverflowpost)</description>
    <licenses>
        <license>
            <name>MIT License</name>
            <url>http://www.opensource.org/licenses/mit-license.php</url>
        </license>
    </licenses>
    <developers>
        <developer>
            <name>(removedforstackoverflowpost)</name>
            <email>(removedforstackoverflowpost)</email>
            <organization>(removedforstackoverflowpost)</organization>
            <organizationUrl>(removedforstackoverflowpost)</organizationUrl>
        </developer>
    </developers>
    <scm>
        <connection>(removedforstackoverflowpost)</connection>
        <developerConnection>(removedforstackoverflowpost)</developerConnection>
        <url>(removedforstackoverflowpost)</url>
    </scm>
    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-console-standalone</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <!-- latest version (2.20.1) does not work well with JUnit5 -->
                <version>2.19.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.0.3</version>
                    </dependency>
                    <dependency>
                        <groupId>org.junit.jupiter</groupId>
                        <artifactId>junit-jupiter-engine</artifactId>
                        <version>5.0.3</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                    <properties>
                        <configurationParameters>
                            junit.jupiter.extensions.autodetection.enabled = true
                        </configurationParameters>
                    </properties>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>11</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <argLine>
                        --illegal-access=permit
                    </argLine>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

我正在运行 java 11

当我输入这个测试命令时:

mvn -ff clean integration-test

我得到这样结束的输出:

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in com.github.(removedforstackoverflowpost)

Results :

Failed tests:
  SomeTest.testSomething1:13 expected: <1> but was: <2>

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

[ERROR] There are test failures.

Please refer to (removedforstackoverflowpost)/target/surefire-reports for the individual test results.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ (removedforstackoverflowpost) ---
[INFO] Building jar: (removedforstackoverflowpost)....-0.0.1.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.360 s
[INFO] Finished at: 2018-12-12T00:45:03-08:00
[INFO] ------------------------------------------------------------------------

我是一个完全的初学者,所以我很抱歉提出一个过于简单的问题。我是 Java/Maven/JUnit5 的新手,但我只是在寻找我应该做些什么来让测试导致非零退出代码。这样,当测试失败时,我的 CI 运行(我碰巧使用了 CircleCI,但这没关系)将显示为失败。

我在 Mac 上工作,我在这张票上描述的不良行为在我的本地 Mac 工作站和 CircleCI 中都以相同的方式发生。

标签: mavenjunit5

解决方案


pom.xml 的第 102 行指示 Maven 忽略失败。只需删除<testFailureIgnore>true</testFailureIgnore>,构建就会失败。


推荐阅读