java - maven-surefire-plugin 不适用于配置文件

标签 java maven junit maven-surefire-plugin

我的 POM 中有配置文件配置,其中包含 Surefire-maven-plugin 和 junit 连接,以便仅按配置文件运行特定测试。例如:

mvn clean test -Pgroup1 --also-make -DfailIfNoTests=false

它在以下版本中按预期工作:

<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
<junit.version>4.12</junit.version>

但是当我尝试升级它们时停止正常工作:

<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
<junit.version>4.13</junit.version>

在这种情况下,mvn test 始终运行所有测试,因为我不会在命令行中设置配置文件。 我的配置文件配置是:

    <profiles>
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>**/unit/*Test.java</include>
                            </includes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>group1</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <exclude>**/unit/**</exclude>
                            </excludes>
                            <groups>com.Group1</groups>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>group2</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <exclude>**/unit/**</exclude>
                            </excludes>
                            <groups>com.Group2</groups>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        ......................
    </profiles>

每个测试类都有链接到配置文件的连接接口(interface):

@Category(Group1.class)
@RunWith(JUnitParamsRunner.class)
public class Group1Test {

使用“默认”配置文件和“activeByDefault”属性也没有给我任何结果。有什么解决办法吗?

最佳答案

我通过在默认插件和配置文件插件中使用“执行”来实现此功能(顺便说一句,这不是默认插件的覆盖)

<project>
  ...
  <build>
  ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M8</version>
        <executions>
          <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals><goal>test</goal></goals> <!-- REQUIRED -->
            <configuration>
              <enableAssertions>true</enableAssertions>
              <systemPropertyVariables>
                <log4j.debug>true</log4j.debug>
                <client.test.url>http://localhost:8080/axis/services/MyService</client.test.url>
              </systemPropertyVariables>
              <excludes>
                <exclude>**/TestAccountOp</exclude> <!-- Tomcat required - use the profile -->
                <exclude>**/TestWsdl</exclude>
              </excludes>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <profiles>
    <profile>
      <id>myprofileid-testwithtomcat</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M8</version>
            <executions>
              <execution> <id>default-test</id> <phase>none</phase> </execution> <!-- Disable default Maven execution -->
              <execution>
                <id>myexecutionid-testwithtomcat</id>
                <phase>test</phase>
                <goals><goal>test</goal></goals> <!-- REQUIRED -->
                <configuration>
                  <enableAssertions>true</enableAssertions>
                  <systemPropertyVariables>
                    <log4j.debug>true</log4j.debug>
                    <client.test.url>http://localhost:8080/axis/services/MyService</client.test.url>
                  </systemPropertyVariables>
                  <excludes>
                    <exclude>**/TestWsdl</exclude>
                  </excludes>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
    ...
  </profiles>
</project>

在配置文件中,我删除了“默认测试”,否则“排除”是从默认设置中设置的,但这可能是在我将“配置”移动到“执行”之前。您必须记住,除非您禁用它,否则“默认测试”是 Activity 的,但在主体中不使用“执行”对我来说不起作用。

“default-test”是 Maven 主体中的“id”,所以这就是为什么我在主体中的“execution”中使用该名称。

我认为您可以不必担心“阶段”元素,因为这是“测试”目标的默认设置,但我很确定您需要该目标。

祝你好运!

关于java - maven-surefire-plugin 不适用于配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63451828/

相关文章:

android - getStartedActivityIntent 在使用 ActivityUnitTestCase 的单元测试用例中返回 null

java - 使用比较器对 Java TreeSet 进行排序

java - 在解决构建路径错误之前无法构建项目(仍然)

java - NoClassDefFoundError ForkedBooter

java - 从 Tycho P2 存储库切换到 Eclipse Target 文件

android - Maven 为构建 android 项目提供了哪些好处(相对于 ant)?

java - 需要将 Nodejs REST api 服务器移植到 java api 服务器

java - 无法使用 jsch 执行 sftp 子命令

java - 如何在junit中设置mockservletcontext的真实路径

java - 为什么 JaCoCo 看到了我的 JUnit 但忽略了我的 Spock 测试?