首页 > 解决方案 > 如何防止 Maven Surefire 自动运行 Spring 集成测试

问题描述

我的集成测试需要很长时间才能运行,我不希望我的开发人员每次需要编译时都浪费时间。我只希望我的集成测试运行:

这可能吗?我怎样才能实现它?

标签: javaspringmavenintegration-testingmaven-surefire-plugin

解决方案


在您的 POM 中添加一个配置,在您的正常构建期间排除集成测试(假设您的集成测试被命名为UserService_IT.java.

    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <skipTests>${skip.surefire.tests}</skipTests>
      <excludes>
        <exclude>**/*_IT*.java</exclude>
      </excludes>
    </configuration>

要运行集成测试,比如说在您的 CI 构建中,添加一个配置文件并在您的管道中激活它,例如mvn verify -Pintegration-tests.

<profile>
  <id>integration-tests</id>
  <activation>
    <activeByDefault>false</activeByDefault>
    <property>
      <name>integration-tests</name>
    </property>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <configuration>
          <includes>
            <include>**/*_IT.java</include>
          </includes>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</profile>

推荐阅读