【问题标题】:maven: execute integration tests with junit 4, overriding settings in parent pommaven:使用 junit 4 执行集成测试,覆盖父 pom 中的设置
【发布时间】:2020-03-20 12:31:57
【问题描述】:

我已迁移到 JUnit 5。在我的集成测试中,我使用的是 PowerMock,不幸的是它还不支持最新的 JUnit 版本(请参阅 https://github.com/powermock/powermock/issues/830)。因此,我决定使用较新版本运行单元测试,该版本在父 POM 中定义为默认版本,并且仅针对 JUnit 4 运行集成测试。我尝试覆盖子 POM 中的插件,但测试不会得到执行,可能是因为测试未被识别为 JUnit 4 测试,因此 maven-failsafe-plugin 找不到任何要执行的集成测试。

父 POM:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.0.0-M4</version>
            <executions>
                <execution>
                    <id>integration-tests</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                    <configuration>
                        <argLine>${failsafeOverridableArgLine}
                            -Djacoco-agent.destfile=${project.build.directory}/coverage-reports/jacoco-it.exec</argLine>
                        <skipTests>${skip.integration.tests}</skipTests>
                        <includes>
                            <include>**/*IntegrationTest.java</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
      ...
      <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.5.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.5.2</version>
        <scope>test</scope>
    </dependency>

子 POM:

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>3.0.0-M4</version>
            <executions>
                <execution>
                    <id>integration-tests</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                    <configuration combine.self="override">
                        <argLine>${failsafeOverridableArgLine}
                            -Djacoco-agent.destfile=${project.build.directory}/coverage-reports/jacoco-it.exec</argLine>
                        <skipTests>${skip.integration.tests}</skipTests>
                        <includes>
                            <include>**/*IntegrationTest.java</include>
                        </includes>
                        <classpathDependencyExcludes>
                            <classpathDependencyExclude>org.junit.jupiter:junit-jupiter</classpathDependencyExclude>
                            <classpathDependencyExclude>org.junit.jupiter:junit-jupiter-engine</classpathDependencyExclude>
                        </classpathDependencyExcludes>
                        <additionalClasspathElements>
                            <additionalClasspathElement>${settings.localRepository}/junit/junit/4.12/junit-4.12.jar</additionalClasspathElement>
                        </additionalClasspathElements>
                    </configuration>
                </execution>
            </executions>
        </plugin>

这个配置正确吗?

【问题讨论】:

    标签: maven-3 junit4 maven-failsafe-plugin


    【解决方案1】:

    问题已解决我需要将以下依赖项添加到覆盖的插件中:

              <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-api</artifactId>
                        <version>3.0.0-M4</version>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>3.0.0-M4</version>
                    </dependency>
                </dependencies>
    

    现在集成测试使用 junit4 运行,而单元测试使用 junit5 运行

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 1970-01-01
      • 2011-12-26
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      • 2012-03-15
      • 1970-01-01
      相关资源
      最近更新 更多