java maven-surefire-plugin是不再工作与配置文件

vcudknz3  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(159)

我在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>

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

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

玩“默认”配置文件和“activeByDefault”属性也没有给我任何结果。有什么想法如何修复它?

68bkxrlz

68bkxrlz1#

我通过在默认插件和配置文件插件中使用"exections"来实现这个功能(顺便说一句,这并不是默认插件的覆盖)

<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>

在配置文件中,我删除了"default-test",否则"excludes"是从默认设置中设置的,但这可能是在我将"配置"移到"执行"之前。您必须记住,"default-test"是活动的,除非您禁用它,但在主体中不使用"执行"对我不起作用。
"default-test"是Maven的"id",表示主体中的一个变量,这就是为什么我在主体中的"execution"中使用这个名称。
我认为您可以不考虑"阶段"元素,因为这是"测试"目标的默认设置,但我非常肯定您需要目标。
祝你好运!

相关问题