Search code examples
mavensonarqubeosgijacocoeasymock

OSGi Code coverage - Jacoco + Easymock


I'm currently working on a Java OSGi project(based on Apache felix runtime) with a project setup like the one below:

  • pkg | parent maven project
  • persistence | real plugin
  • persistence.tests | test plugin (indeed a Fragment project with fragment host the persistence plugin above )
  • ... others like the ones above

Basically I use maven + tycho to manage the lifecycle of the project. the whole stuff flows through a continuos integration pipeline which involves jenkins for building, testing, deploying and forwarding code analysis to a Sonarqube server. Just like mentioned above, tests are implemented through Fragment projects pointing OSGi bundles to be tested. In these tests I make use of EasyMock library to generate mocked OSGi bundles. In order to make Sonarqube aware of tests coverage I had to add Jacoco (maven plugin) into my sets of tools. After a few adjustments to the configuration of my parent pom.xml file I ended up with something that is working partially: jacoco code coverage is only working for classes included in test plugins (the fragment projects). As you may guess - though better than nothing - this result is far from being useful. I need to evalute test coverage on real OSGi bundles. After some googling I understood that the problem could be linked to the usage of EasyMock library, since this alterate original classes during execution causing a mismatch between test classes and real classes. According to my understanding, to solve I need to disable jacoco on-the-fly instrumentation and use offline instrumentation instead.

Nevertheless I'm not able to understand :

  • what does this really means
  • how to do it

Can someone kindly revert on this ?

This is the maven command i'm running to generate jacoco report

mvn -f com.mycompany.osgi.myproject.pkg/pom.xml clean test

Below my current parent pom.xml

<?xml version="1.0" encoding="UTF-8"?><project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.osgi</groupId>
<artifactId>com.mycompany.osgi.myproject.pkg</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>

<properties>
    <tycho.version>1.0.0</tycho.version>
    <surefire.version>2.16</surefire.version>
    <main.basedir>${project.basedir}</main.basedir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jacoco.version>0.7.9</jacoco.version>

    <!-- Sonar-JaCoCo properties -->
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<sonar.junit.reportPaths>${project.basedir}/target/surefire-reports</sonar.junit.reportPaths>
<sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
</properties>


<modules>

    <module>../com.mycompany.osgi.myproject.core.persistence</module>
    <module>../com.mycompany.osgi.myproject.core.persistence.tests</module> 

</modules>

<build>
        <plugins>
            <plugin>
          <groupId>org.jacoco</groupId>
          <artifactId>jacoco-maven-plugin</artifactId>
          <version>${jacoco.version}</version>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-compiler-plugin</artifactId>
                <version>${tycho.version}</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.eclipse.tycho</groupId>
                <artifactId>tycho-maven-plugin</artifactId>
                <version>${tycho.version}</version>
                <extensions>true</extensions>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${surefire.version}</version>


                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.surefire</groupId>
                        <artifactId>surefire-junit47</artifactId>
                        <version>${surefire.version}</version>
                    </dependency>
                </dependencies>

                <executions>
                    <execution>
                        <id>test</id>
                        <phase>test</phase>
                        <configuration>
                            <testClassesDirectory>${project.build.outputDirectory}</testClassesDirectory>
                        </configuration>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
</build>


<repositories>
    ...
</repositories>

<distributionManagement>
    ...
</distributionManagement>


Solution

  • As suggested by @Godin, my problems were solved using the following jacoco plugin configurations

    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco.version}</version>
        <configuration>
            <dataFile>../com.mycompany.myproject.pkg/target/jacoco.exec</dataFile>
            <destFile>../com.mycompany.myproject.pkg/target/jacoco.exec</destFile>
            <outputDirectory>../com.mycompany.myproject.pkg/target/site/jacoco</outputDirectory>
        </configuration>
    </plugin>
    

    and this project configuration to instruct sonarqube to read expected resources

    <properties>
            ...
    
            <!-- Sonar-JaCoCo properties -->
            <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
    <sonar.junit.reportPaths>com.mycompany.myproject.pkg/target/surefire-reports</sonar.junit.reportPaths>
            <sonar.jacoco.reportPaths>com.mycompany.myproject.pkg/target/jacoco.exec</sonar.jacoco.reportPaths>
        </properties>