In Maven is it possible to keep integration tests

2020-02-26 04:03发布

问题:

On the Maven and Integration Testing page it says:

The Future Rumor has it that a future version of Maven will support something like src/it/java in the integration-test phase, in addition to src/test/java in the test phase.

but that was back in 2011-12-11. Has this happened yet?

In this answer to "Run maven test not in default src/test/java folder" it mentions setting the <testSourceDirectory>, is their some way of doing this just for integration test (ie. the integration-test phase)?

I'm looking to use the Maven FailSafe plugin and avoid renaming a bunch of integration tests or using the still experimental JUnit @Categories.

回答1:

You can put the IT'ss into different folder like this:

.
|-- pom.xml
`-- src
    |-- it
    |   `-- java
    |       `-- com
    |           `-- soebes
    |               `-- maui
    |                   `-- it
    |                       `-- BitMaskIT.java
    |-- main
    |   `-- java
    |       `-- com
    |           `-- soebes
    |               `-- maui
    |                   `-- it
    |                       `-- BitMask.java
    `-- test
        `-- java
            `-- com
                `-- soebes
                    `-- maui
                        `-- it
                            `-- BitMaskTest.java

The following is needed to make then folders known to the compiler etc.

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.5</version>
  <executions>
    <execution>
      <id>add-test-source</id>
      <phase>process-resources</phase>
      <goals>
        <goal>add-test-source</goal>
      </goals>
      <configuration>
        <sources>
          <source>src/it/java</source>
        </sources>
      </configuration>
    </execution>
  </executions>
</plugin>

The following is needed to really run the IT's:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.15</version>
  <executions>
    <execution>
      <id>integration-test</id>
      <goals>
        <goal>integration-test</goal>
      </goals>
    </execution>
    <execution>
      <id>verify</id>
      <goals>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
</plugin>

This means you can have the integration within the same module which has the disadvantage that running the integration tests use the same resources as the unit tests. A better solution would be to create a separate maven module where you can put the integration tests into the usual folder src/test/java etc. and only configure the maven-failsafe-plugin.