Search code examples
mavenmaven-surefire-pluginmaven-failsafe-plugin

Maven failsafe doesn't read environmentVariables config for integration tests



My Maven build comprises of unit and integrations test executions as 2 separate profiles with Jacoco for code-coverage.
I am having couple of problems with failsafe plugin and require a direction to look for resolutions. I've already snailed through numerous content available but couldn't resolve the problem yet.

  1. <environmentVariables> do not work for failsafe, while it does for surefire . I've tried everything from <systemProperties>,<systenPropertiesVariables>,<properties>; but none seem to work to set environment variables for my Integrations test cases.
  2. Test case exclusions using <excludes> is not working for failsafe; where as it does for surefire.

My failsafe configuration is:

<profile>
    <id>it-coverage</id>
    <build>
        <plugins>
            <!-- MAVEN Failsafe plugin. -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                    <argLine>${failsafeArgLine}</argLine>
                    <environmentVariables>
                        <config>config/preferences.xml</config>
                        <log4jproperties>config/log4j.properties</log4jproperties>
                        <jacoco-agent.destfile>target/it-jacoco.exec</jacoco-agent.destfile>
                    </environmentVariables>
                    <excludes>
                        <exclude>**/*UTest.java</exclude>
                    </excludes> 
                    <includes>
                        <include>**/*ITest.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</profile>

Solution

  • Well, my problems are resolved. I added surefire-plugin to my integration profile along with failsafe-plugin and everything started falling in place.

    Below is the configuration which worked for me.

    <profile>
        <id>it-coverage</id>
        <build>
            <plugins>
                <!-- MAVEN Failsafe plugin. -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.20.1</version>
                    <executions>
                        <execution>
                            <id>integration-test</id>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <!-- Sets the VM argument line used when unit tests are run. -->
                        <argLine>${failsafeArgLine}</argLine>
                        <systemPropertyVariables>
                            <config>${config.preferences}</config>
                            <log4jproperties>${config.log4jproperties}</log4jproperties>
                            <!-- <jacoco-agent.destfile>target/it-jacoco.exec</jacoco-agent.destfile> -->
                            <jacoco-agent.destfile>${sonar.jacoco.itReportPath}</jacoco-agent.destfile>
                        </systemPropertyVariables>
                        <includes>
                            <include>**/*ITest.java</include>
                        </includes>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <skip>true</skip>
                    </configuration>
                </plugin>
    
                <!-- MAVEN Jacoco plugin -->
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>${jacoco.version}</version>
                    <configuration>
                        <excludes>
                            <exclude>**/*UTest.java</exclude>
                        </excludes>
                    </configuration>
                    <executions>
                        <execution> 
                            <id>default-instrument</id>
                            <goals>
                                <goal>instrument</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>default-restore-instrumented-classes</id>
                            <goals>
                                <goal>restore-instrumented-classes</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>default-report</id>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.jacoco</groupId>
                <artifactId>org.jacoco.agent</artifactId>
                <classifier>runtime</classifier>
                <version>${jacoco.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </profile>