11

I'm using JUnit Categories to separate integration tests from unit tests. The Surefire plugin configuration works - it skips the tests annotated with my marker interface IntegrationTest.

However, the Failsafe plugin doesn't pick the integration tests. I've even tried to specify the junit47 provider, but zero tests are run in the integration-test phase.

Here is the pom.xml fragment:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.12</version>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <groups>com.mycompany.test.IntegrationTest</groups>
                <excludedGroups>com.mycompany.test.UnitTest</excludedGroups>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>2.12</version>
                </dependency>
            </dependencies>
        </plugin>

Here is the Failsafe part of the log:

[INFO] --- maven-failsafe-plugin:2.12:integration-test (default) @ MyProject.war ---
[INFO] Failsafe report directory: /home/stoupa/MyProject/war/target/failsafe-reports
[INFO] Using configured provider org.apache.maven.surefire.junitcore.JUnitCoreProvider

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Concurrency config is parallel='none', perCoreThreadCount=true, threadCount=2, useUnlimitedThreads=false

Results :

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

Is the provider org.apache.maven.surefire.junitcore.JUnitCoreProvider that can be seen in the log output the right one?

raksja
  • 3,969
  • 5
  • 38
  • 44
brabec
  • 4,632
  • 8
  • 37
  • 63
  • Why don't you use the naming conventions for integration tests of maven-failsafe-plugin ? Might be simpler. – khmarbaise Jul 18 '12 at 13:44
  • 2
    I'm experiencing the same here. when replacing failsafe with surefire everything works fine... – Boaz Oct 25 '12 at 15:01

2 Answers2

16

By default, the failsafe plugin excludes various files. You have to override that. So change your configuration section to the following:

<configuration>
    <includes>
        <include>**/*.java</include>
    </includes>
    <groups>com.mycompany.test.IntegrationTest</groups>
    <excludedGroups>com.mycompany.test.UnitTest</excludedGroups>
</configuration>
tunesmith
  • 1,030
  • 9
  • 22
  • Yes, by default, it only includes Java files matching certain patterns used for integration tests (e.g. "*IT.java"). If you use categories instead of filename matching, you need to remove the filename matching criteria. – Scott McIntyre Feb 17 '17 at 14:40
1

WORKAROUND

@Categories will give pain as you have to mark each of your integration tests.

Try creating a inttests profile as mentioned below and skip the surefire execution.

 <profile>
        <id>inttests</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <build>
            <plugins>
                <!-- skip the unit tests -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <configuration>
                        <includes>
                            <include>**/IT*.java</include>
                        </includes>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

Prefix or suffix the Integration tests with some string and give that in the includes. By default failsafe picks up all the tests prefixed by IT as the integration tests. Ref: Failsafe Inclusions & Exclusions

Run it using the maven profile command

mvn verify -P inttests

Note: In the above mentioned approach, we need to run the unit tests during build and then inttests separately.

Updates: On JUnit4 Categories With Failsafe Plugin

  • 2.12 - The forked VM terminated without saying properly goodbye. VM crash or System.exit called ? https://issues.apache.org/jira/browse/SUREFIRE-827
  • 2.12.1 - Unable to locate surefire-booter in the list of plugin artifacts https://issues.apache.org/jira/browse/SUREFIRE-896
  • 2.12.2 - has a bug when no tests are there in a module it will look for failsafe-summary.xml and fail https://issues.apache.org/jira/browse/SUREFIRE-901
  • user482745
    • 1,165
    • 1
    • 11
    • 31
    raksja
    • 3,969
    • 5
    • 38
    • 44
    • 3
      You don't need to mark each test, is enough with creatting an annotated superclass and inherit from it.Anyway I dont see how annotating each class is more a pain that having them named IT* – hithwen Feb 12 '13 at 11:16
    • 1
      This answer doesn't really answer the question, it's a workaround. I'm having the same problem. Is it a bug? – Jörgen Lundberg May 28 '13 at 06:27
    • 1
      Same problem here...anybody know of a solution? works however! – tk2000 Jun 07 '13 at 11:54
    • @hithwen i mentioned about the pain in resolving the issues that we are facing because of Categories annotation with failsafe. I just edited this as a workaround and this is how I tried to resolve it. – raksja Jan 31 '14 at 00:38