为什么我的 maven-surefire-plugin 需要放在主项目构建中?

Why my maven-surefire-plugin needs to sit in the main project build?

请注意,我对主项目构建中的 maven-surefire-plugin 感到满意,因为它为我提供了所需的成功构建。

但是我还有一个问题让我很困惑...

我正在 运行构建 Maven 项目。有两种测试。集成测试。在创建和部署 war 存档之前,这些测试 运行 针对外部和完全空的数据库。有12个这样的测试。和验收测试。这些测试 运行 针对外部和非空数据库,在创建和部署后使用 war 存档。有1个这样的测试。

现在,当 运行 命令时:

mvn clean install -Denv="test"

可以看到12个集成测试已经通过,构建成功。

但是当运行执行命令时:

mvn clean install -Denv="acceptance"

我可以看到考虑了 13 个测试,构建失败并显示控制台日志:

Tests in error: 
  testUser(it.kahoot.robot.rest.acceptance.UserControllerTest): I/O error on POST request for "http://localhost:8080/kahoot-rest/api/users/login":Connection refused; nested exception is java.net.ConnectException: Connection refused

Tests run: 13, Failures: 0, Errors: 1, Skipped: 0

测试结果表明构建尝试 运行 在创建和部署 war 存档之前进行验收测试。

-------------------------------------------------------------------------------
Test set: it.kahoot.robot.rest.acceptance.UserControllerTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 20.388 sec <<< FAILURE!
testUser(it.kahoot.robot.rest.acceptance.UserControllerTest)  Time elapsed: 0.407 sec  <<< ERROR!
org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost:8080/kahoot-rest/api/users/login":Connection refused; nested exception is java.net.ConnectException: Connection refused

包的拆分方式如下: stephane@stephane-ThinkPad-X301:rest> 树

.
├── acceptance
│   ├── AbstractControllerTest.java
│   ├── BaseControllerTest.java
│   └── UserControllerTest.java
├── assertion
│   ├── PartResourceAssert.java
│   ├── UserResourceAssert.java
│   └── UserRoleResourceAssert.java
└── integration
    ├── AbstractControllerTest.java
    ├── BaseControllerTest.java
    ├── UserControllerTest.java
    ├── UserExceptionTest.java
    └── WebSecurityTestConfiguration.java

我的 pom.xml 文件有一个用于集成测试的 "test" 配置文件和一个用于验收测试的 "acceptance" 配置文件。

<profile>
  <id>test</id>
  <activation>
    <property>
      <name>env</name>
      <value>test</value>
    </property>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.17</version>
        <configuration>
          <includes>
            <include>**/integration/*.java</include>
          </includes>
          <excludes>
            <exclude>**/acceptance/*.java</exclude>
          </excludes>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>
<profile>
  <id>acceptance</id>
  <activation>
    <property>
      <name>env</name>
      <value>acceptance</value>
    </property>
  </activation>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>failsafe-maven-plugin</artifactId>
        <version>2.4.3-alpha-1</version>
        <configuration>
          <includes>
            <include>**/acceptance/*.java</include>
          </includes>
          <excludes>
            <exclude>**/integration/*.java</exclude>
          </excludes>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.3.4.RC0</version>
        <executions>
          <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <goals>
              <goal>start</goal>
            </goals>
          </execution>
          <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>
            <goals>
              <goal>stop</goal>
            </goals>
            <configuration>
              <stopKey>stop</stopKey>
              <stopPort>8081</stopPort>
            </configuration>
          </execution>
        </executions>
        <configuration>
          <scanIntervalSeconds>10</scanIntervalSeconds>
          <webApp>
            <contextPath>/kahoot-rest</contextPath>
          </webApp>
          <daemon>true</daemon>
          <connectors>
            <connector
              implementation="org.eclipse.jetty.nio.SelectChannelConnector">
              <port>8080</port>
              <maxIdleTime>300000</maxIdleTime>
            </connector>
          </connectors>
        </configuration>
      </plugin>
    </plugins>
  </build>
</profile>

有趣的一点是:当我将 maven-surefire-plugin 从 "test" 配置文件移出并进入主项目构建时,这两个命令最终都会成功构建。 以下插件:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <configuration>
      <includes>
        <include>**/integration/*.java</include>
      </includes>
      <excludes>
        <exclude>**/acceptance/*.java</exclude>
      </excludes>
    </configuration>
  </plugin>

被移出 "test" 配置文件并放置在主项目构建中,这导致两个命令都成功构建。

命名模式记录在相应的插件页面中:

我会将验收测试放到一个单独的项目中,并使用配置文件定义执行。

集成测试可以 运行 由适当的 life cylcle phaseintegration-test

但是根据您的信息,您希望为集成测试启动一个码头,因此为集成测试制作一个单独的模块也是有意义的,该模块可以通过配置文件激活。

将它们分开的另一个重要点是有一个单独的 class 路径用于集成测试和验收测试,这可能意味着具有不同的依赖关系并且可能是某种配置(属性 文件等。 ) 基于测试。