16

My directory structure is like so:

  • src/integrationTest/java
  • src/test/java
  • src/main/java

I am trying to get failsafe to pick-up the integration tests, but failing to do so in the way I would like.

I have tried this:

<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.17</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <testSourceDirectory>src/integrationTest/java</testSourceDirectory>
        <testClassesDirectory>${project.build.directory}/it-classes</testClassesDirectory>
    </configuration>
</plugin>

and this:

<plugin>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.17</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <includes>
            <include>src/integrationTest/**/*.java</include>
        </includes>
    </configuration>
</plugin>

to no avail; failsafe does not find tests to run.

I have been able to use the build-helper-maven plugin to add a test-source directory and then failsafe runs the tests.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9.1</version>
    <executions>
        <execution>
            <id>add-integration-test-source-as-test-sources</id>
            <phase>generate-test-sources</phase>
            <goals>
                <goal>add-test-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/integrationTest/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

However, the problem now is that surefire now also runs the tests as unit-tests. Also it seems unnecessary to use another plugin when failsafe should be able to find the tests; I would prefer to not to need to use build-helper maven plugin and configure surefire to exclude that path.

What am I doing wrong?

Raedwald
  • 46,613
  • 43
  • 151
  • 237
neuronotic
  • 487
  • 7
  • 18

2 Answers2

10

By default failsafe is configured to only include IT*.java, *IT.java or *ITCase.java. While at the same time, only test sources from src/test/java are compiled. You need to modify both of these behaviors.

  1. Use build-helper-maven-plugin to add src/integationTest/java as test source for maven-compiler-plugin to pick up automatically. (You've already done this in your last attempt.)

  2. Direct maven-surefire-plugin to exclude your integration tests (see example below) or to include only non-integration tests (see default includes).

  3. Direct maven-failsafe-plugin to only include your integration tests instead of default includes.


<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.17</version>
  <configuration>
    <excludes>
      <exclude>**/*Stuff.java</exclude>
    </excludes>
  </configuration>
</plugin>
<plugin>
  <artifactId>maven-failsafe-plugin</artifactId>
  <version>2.17</version>
  <executions>
    <execution>
      <goals>
        <goal>integration-test</goal>
        <goal>verify</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <includes>
      <include>**/*Stuff.java</include>
    </includes>
  </configuration>
</plugin>

In fact using testClassesDirectory might also work for limiting scope of each test plugin, but you would have to make changes to maven-compiler-plugin to output classes to different folders, perhaps splitting it's test-compile execution into two, so maybe it's not worth the effort.

Anton Koscejev
  • 4,603
  • 1
  • 21
  • 26
  • Of course the minimal configuration convention is to keep your integration test classes in `src/test/java` and rename them according to those naming patterns: `IT*.java`, `*IT.java` or `*ITCase.java`. – Anton Koscejev Jul 15 '16 at 14:22
  • The above doesn't appear to be working :( I agree wrt naming convention; but not possible in this case at the present time. – neuronotic Jul 15 '16 at 14:53
  • Please verify that you have `.class` files in that folder. Normally `maven-compiler-plugin` generates them during `test-compile` phase, but that applies to `src/test/java` folder. If you're using a different folder, I assume you would need to add an additional compilation step. – Anton Koscejev Jul 15 '16 at 15:57
  • Indeed, compilation is what's most likely you're missing, which is why `build-helper-maven-plugin` helped, since it provided `maven-compiler-plugin` with information about additional classes to compile. I'll post a different answer related to that... – Anton Koscejev Jul 15 '16 at 16:00
  • Wow, even for TestNG the default is *IT files only. Mind blown... – rogerdpack Dec 06 '21 at 17:32
  • And if you have resources for those tests outside the usual path you should include te goal "add-test-resource" in your "build-helper-maven-plugin" and indicate the path of those resources. – Joselo Oct 03 '22 at 11:26
0

Got working integration tests on Groovy residing in src/test/groovy with this configuration:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.0.0-M5</version>
    <executions>
        <execution>
            <id>spock-integration-tests</id>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <failIfNoTests>true</failIfNoTests>
                <testSourceDirectory>${project.basedir}/src/test/groovy</testSourceDirectory>
                <includes>
                    <include>**/*Specification</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>

Note that pattern in <include> doesn't contain file extension (**/*Specification.groovy doesn't work)

Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74