Using JUnit Categories with Maven Failsafe plugin

2019-03-25 07:23发布

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?

2条回答
Juvenile、少年°
2楼-- · 2019-03-25 07:27

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
  • 查看更多
    forever°为你锁心
    3楼-- · 2019-03-25 07:53

    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>
    
    查看更多
    登录 后发表回答