Search code examples
mavenjacocomaven-surefire-pluginjacoco-maven-plugin

Why no jacoco.exec produced when a single test is run - but it is produced when all tests are run?


I'm running surefire tests with a jacoco agent. When I run mvn verify a jacoco.exec file is produced.

When I run mvn clean verify -Dtest=com.org.MyTest -DfailIfNoTests=false then no jacoco.exec file is produced.

Here is my surefire config.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
    </configuration>
    <executions>
        <execution>
            <phase>test</phase>
            <id>testconfig</id>
            <configuration>
                <argLine>${test.jvm.options} ${jacoco.agent.argLine}</argLine>
                <skip>false</skip>
            </configuration>
            <goals><goal>test</goal></goals>
        </execution>
    </executions>
</plugin>

Here is my jacoco config

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.5.201505241946</version>
    <configuration>
        <properties>
            <property>
                <name>listener</name>
                <value>org.sonar.java.jacoco.JUnitListener</value>
            </property>
        </properties>
    </configuration>
    <executions>
        <execution>
            <id>unit_agent</id>
            <phase>initialize</phase>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <propertyName>jacoco.agent.argLine</propertyName>
            </configuration>
        </execution>                           
    </executions>
</plugin>

My question is: Why no jacoco.exec produced when a single test is run - but it is produced when all tests are run?


Solution

  • Log of execution of mvn clean verify -Dtest=com.org.MyTest -DfailIfNoTests=false shows something like (I'm using Apache Maven 3.3.9):

    [INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ example ---
    [INFO] Surefire report directory: /private/tmp/jacoco-example/target/surefire-reports
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.org.MyTest
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec - in com.org.MyTest
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO]
    [INFO] --- maven-surefire-plugin:2.18.1:test (testconfig) @ example ---
    [INFO] Skipping execution of surefire because it has already been run for this configuration
    

    Notice that maven-surefire-plugin executed two times - one time with id default-test and another execution with id testconfig is actually skipped, while only configuration with id testconfig uses ${jacoco.agent.argLine}.

    Change of definition for maven-surefire-plugin on

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
            <argLine>${jacoco.agent.argLine}</argLine>
        </configuration>
    </plugin>
    

    solves the issue.