Michael S
Michael S

Reputation: 779

Maven surefire configuration for separating test into unit and integration tests fail

I have a project which contain unit and integration tests. These tests are already split into unit and integration tests while using different class as suites.

How can I configure surefire to execute unit tests during phase "test" and integration test during phase "integration-Test".

Here is my current configuration:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <excludes>
                        <exclude>**/Test*.java</exclude>
                        <exclude>**/*Test.java</exclude>
                        <exclude>**/*Test*.java</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <id>unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>**/CoreUnitTests.java</include>
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>integration-test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>**/CoreIntegrationTests.java</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

With this configuration no tests will be executed but if I remove the "excludes" all tests are executed in phase "test" and not only the unit tests.


Update - Solution

With the explanation of Adam Michalik I was able to solve the problem. Instead of overriding default-test I skip these tests because id "unit-test" is better than "default-test" for my unit tests Here is the final configuration of the pom:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <executions>
                    <execution>
                        <id>default-test</id>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </execution>
                    <execution>
                        <id>unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>**/CoreUnitTests.java</include>
                            </includes>
                        </configuration>
                    </execution>
                    <execution>
                        <id>integration-test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <includes>
                                <include>**/CoreIntegrationTests.java</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

Upvotes: 2

Views: 1780

Answers (1)

Adam Michalik
Adam Michalik

Reputation: 9965

Rule of thumb: excludes override includes. So, taking the unit-test execution as an example, your configuration tells Surefire to:

  • first of all, only include **/CoreUnitTests.java
  • but from that, exclude **/Test*.java, **/Test.java and **/*Test*.java - that exclusion list covers CoreUnitTests.java, so nothing is executed

Similarly for the integration-test execution.

So, to only run CoreUnitTests in the test phase and CoreIntegrationTests in the integration-test phase, is should be enough to configure the includes as you did and do not define any excludes anywhere. That is however not good enough, because, as you observed, all tests are run by the default Surefire execution called default-test, which, by default configuration, picks up all classes suffixed with Test. To work around this, you can either turn off that default execution by reconfiguring it

<execution>
    <id>default-test</id>
    <configuration>
        <skip>true</skip>
    </configuration>
</execution>

or rename your unit-test execution to default-test (that will save a few milliseconds of your build time, because in the end you'll only invoke Surefire twice, not three times just to skip the default execution).

Upvotes: 5

Related Questions