35

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.

Community
  • 1
  • 1
Sled
  • 18,541
  • 27
  • 119
  • 168
  • Here is another answer that is up to date with latest maven: https://stackoverflow.com/questions/46831999/maven-add-integration-tests – froderik Apr 17 '19 at 08:47

1 Answers1

38

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.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Nice! Just to be clear both test and it will be scanned for unit tests and integration tests so it will fundamentally still depend on the filename to distinguish between the two right? – Sled Jul 25 '13 at 19:10
  • For the first scenario correct. But the better is having a different module where you can change the naming convention. I wouldn't recommend that. – khmarbaise Jul 26 '13 at 05:33
  • Why does the failsafe plugin run during `verify` instead of `integration-test`? It seems odd for failsafe to run after `post-integration-test`? – Mike Samuel Mar 11 '16 at 13:47
  • It looks like the same `build-helper-maven-plugin` invocation can be used with `add-test-resource` to add `src/it/resources` to the list of test resources directories. – Mike Samuel Mar 11 '16 at 13:54
  • @khmarbaise In your first example, I understand putting `BitMaskIT` into a package named `com.soebes.maui.it` but not `BitMaskTest` and `BitMask`. I would expect those classes to be in a class called `com.soebes.maui`, right? – mattalxndr Jun 08 '16 at 00:17
  • @mattalxndr You can do of course. But the package name shouldn't be a criteria. Best is to make a separate module for IT's. – khmarbaise Jun 08 '16 at 11:34
  • @khmarbaise I'm not implying that it would change the results, but in the example, you might as well put `BitMask` and `BitMaskTest` classes in the most likely package, right? The fact that they are in the `.it` package leads me to wonder if they need to be, since it doesn't make sense why they would be. – mattalxndr Jun 08 '16 at 20:30
  • Ah sorry. misunderstanding on my site. You can of course put the IT's in a package what ever you like. I just wanted to make clear that they are not in the same package as the unit tests cause IT's should have access to package private classes. – khmarbaise Jun 09 '16 at 09:55
  • @khmarbaise I have both groovy test and java tests in a module named `integrationtests`. When I run `mvn test` or `mvn verify` it only runs java tests while groovy (spock) tests are also compiled in test-classes. why is that? I am using both fail-safe and surefire plugin like above. – sansari Jul 06 '18 at 19:03
  • Rather than adding test sources in the ```process-resources``` phase, wouldn't it make more sense to do it in the ```generate-test-sources``` phase? – jd96 Nov 01 '20 at 01:06