【问题标题】:Cobertura+Failsafe: make "mvn clean site" proceed even if an integration test failsCobertura+Failsafe:即使集成测试失败,也可以继续执行“mvn clean site”
【发布时间】:2019-04-14 12:50:37
【问题描述】:

我有一个涉及maven-failsafe-plugin 的Java8/Maven/Spring Boot 项目(为了在*IT.java 中运行集成测试,与*Test.java 中的单元测试分开)和cobertura-maven-plugin

问题在于,如果集成测试失败,mvn clean site 无法得出结论,而如果单元测试失败,mvn clean site 则可以。

为了重现这一点,我首先添加了一个src/test/java/app/FailingTest.java,其中包含:

package app;

import org.junit.Test;

import static org.junit.Assert.fail;

public class FailingTest {

    @Test
    public void failingTest () {
        fail();
    }
}

mvn clean site 产生:

...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running app.FailingTest
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.063 s <<< FAILURE! - in app.FailingTest
[ERROR] failingTest(app.FailingTest)  Time elapsed: 0.005 s  <<< FAILURE!
java.lang.AssertionError
    at app.FailingTest.failingTest(FailingTest.java:11)
...
[INFO] Cobertura Report generation was successful.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

然后我将该类重命名为src/test/java/app/FailingIT.javamvn clean site 产生:

...
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running app.FailingIT
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.031 s <<< FAILURE! - in app.FailingIT
[ERROR] failingTest(app.FailingIT)  Time elapsed: 0.005 s  <<< FAILURE!
java.lang.AssertionError
    at app.FailingIT.failingTest(FailingIT.java:11)
...
[INFO] --- maven-failsafe-plugin:2.22.1:verify (default) @ example-project ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

如何解决这个问题?

我的pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>example-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>example-project</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.16.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-browser</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- This is needed for the failsafe plugin, cf. https://stackoverflow.com/a/42128153/9164010 -->
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
                <!-- This is needed to avoid a compilation error, cf. below -->
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
            <!-- This is needed to avoid a compilation error, cf. https://stackoverflow.com/a/50661649/9164010 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <reporting>
        <plugins>
            <!-- This is needed to avoid a compilation error, cf. https://stackoverflow.com/a/51099913/9164010 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>2.9</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jxr-plugin</artifactId>
                <version>2.5</version>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.7</version>
            </plugin>
        </plugins>
    </reporting>
</project>

【问题讨论】:

    标签: java maven java-8 maven-failsafe-plugin maven-cobertura-plugin


    【解决方案1】:

    这是导致构建失败的maven-failsafe-plugin。原因在您的pom.xml 中明确指出:

    <goal>integration-test</goal>
    

    如果测试真的是集成测试(IT),你必须修复它,否则如果它不是集成测试,你可以将它重命名为单元测试(UT)但仍然修复它:)

    消除此错误的另一种糟糕方法是在pom.xml 中将故障安全插件声明全部注释掉,使其处于非活动状态。不过我建议不要这样做。

    【讨论】:

    • 感谢您的回答,但它并没有真正解决我的问题:我希望 mvn verify 由于 integration-test 目标而失败,但 mvn site 继续生成单元测试的覆盖率统计信息和/或集成测试,即使集成测试失败。基本上,关于单元测试和集成测试的“退出状态”,cobertura+surefire 和 cobertura+failsafe 之间似乎存在一些“不对称”。有可能解决这个问题吗? (不幸的是,故障保护和 cobertura 文档中似乎没有涵盖这种情况)
    • @ErikMD 你在寻找like skipping tests的东西吗?还要质疑基本的,为什么你需要故障安全插件呢?
    • 我使用故障保护插件通过mvn verify 运行集成测试。我在网上看到过一些示例,显示也可以将surefire 配置为运行集成测试,但failsafe doc 表示为此目的使用故障安全比使用surefire 更好。我真的不想跳过测试,我只是希望mvn site 在集成测试失败时不会失败(与目前相同,如果单元测试失败,mvn site 不会失败),以获得单元+集成测试 cobertura 报告。很高兴知道实现这一目标的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多