0

Have followed The instructions in https://github.com/jfcorugedo/sonar-scanner/blob/master/pom.xml and also used jacoco offline instrumentation as mentioned in Powermock documentation but when I use the below configuration the code coverage shows only the power mock code coverage and does not show the other non - powermock (mockito and Junit ) coverages.

Is there a way that I can implement both powermock, mockito and Junit code coverage at the same time

Below is my pom file that would not show powermock coverage.

<properties>
       <powermock.version>2.0.9</powermock.version>
       <jacoco.version>0.8.5</jacoco.version>
    </properties>
  <dependencies>
    <dependencies>
            
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.4</version>
            <scope>test</scope>
        </dependency>
        <!-- Power Mock -->
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-javaagent</artifactId>
            <version>${powermock.version}</version>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4-rule-agent</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build> 
     <plugins>
        <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>PACKAGE</element>
                                    <limits>
                                        <limit>
                                            <counter>LINE</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>0.0</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    </plugins>
    </build>

Below is the POM file(Jacoco offline instrumentation) that works with Powermock but ignores all other test cases. The argline command works for surefire 2.19.1 but does not seem to work on higher versions .

    <properties>
       <powermock.version>2.0.9</powermock.version>
       <jacoco.version>0.8.5</jacoco.version>
    </properties>
  <dependencies>
            
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.4</version>
            <scope>test</scope>
        </dependency>
        <!-- Power Mock -->
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-javaagent</artifactId>
            <version>${powermock.version}</version>
        </dependency>

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4-rule-agent</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <plugins>
      <plugin>
       <groupId>org.jacoco</groupId>
       <artifactId>jacoco-maven-plugin</artifactId>
       <version>${jacoco.version}</version>
       <executions>
          <!--Jacoco offline instrumentation-->
          <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>report</id>
             <phase>prepare-package</phase>
             <goals>
                <goal>report</goal>
             </goals>
             <configuration>
                <dataFile>${project.build.directory}/coverage.exec</dataFile>
             </configuration>
          </execution>
          <execution>
             <id>jacoco-check</id>
             <goals>
                <goal>check</goal>
             </goals>
             <configuration>
                <rules>
                   <rule>
                      <element>PACKAGE</element>
                      <limits>
                         <limit>
                            <counter>LINE</counter>
                            <value>COVEREDRATIO</value>
                            <minimum>0.0</minimum>
                         </limit>
                      </limits>
                   </rule>
                </rules>
             </configuration>
          </execution>
       </executions>
    </plugin>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.19.1</version>
     <configuration>
        <argLine>
        -javaagent:${settings.localRepository}/org/powermock/powermock-module-javaagent/${powermock.version}/powermock-module-javaagent-${powermock.version}.jar
         </argLine>
     <systemPropertyVariables>
     <jacoco-agent.destfile>${project.build.directory}/coverage.exec</jacoco-agent.destfile>
     </systemPropertyVariables>
     </configuration>
      </plugin>
    </plugins>

Have been stuck with this issue for quite a while , I tried adding the @{argline} annotation in the mavensuefire plugin configuration along with javaagent command but does not seem to work either. The code I am working on requires powermock as they have multiple async calls which needs to mocked using withNew. using cobretura is not an option either.

avinash v p
  • 531
  • 2
  • 8
  • 15

1 Answers1

0

Along with offline instrument, we also need to make sure the correct version has been set to the -source and -target of the Java Compiler. This also seems impacts the coverage report as sometime only offline instrument settings doesn't help to get power mock test coverages.

<properties>
        ....
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        .....
</properties>   
..
..

<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                    <configuration>
                  <source>${maven.compiler.source}</source>
                  <target>${maven.compiler.target}</target>
                </configuration>
</plugin>
SarangRN
  • 91
  • 13