Skip to content
Advertisement

Kotlin, Java 8 and Sonar coverage showing as 0

I have a kotlin project using Java 8 and I use Sonar to measure code coverage I am using the latest version of Sonar and noticed the coverage has gone down due to changes in the openjdk. My tests are using Mockito and PowerMockito. Some tests are failing due to reflection errors.

When I add the following jvm args – the errors go away – –add-opens java.base/jdk.internal.loader=ALL-UNNAMED

However, if I add the maven-surefire-plugin to my pom.xml – the code coverage shows as 0 in Sonar

I use Jacoco to measure the coverage – here is the relevant section of my pom.xml

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!--https://stackoverflow.com/questions/39750348/how-to-add-vm-args-using-pom-xml-plugin/39751176 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <argLine>--add-opens java.base/jdk.internal.loader=ALL-UNNAMED</argLine>
                </configuration>
            </plugin>

So if I dont include the surefire plugin, my sonar code coverage shows up but some tests fail due to the above mentioned error. If I include the surefire plugin, all my tests pass but my code coverage shows as 0 in sonar

I am using the following command to run my tests and analyze with Sonar

mvn clean install -DskipTests=false -Dmaven.test.failure.ignore=true sonar:sonar

Is there anyway the surefire plugin and jacoco plugin can co exist so as to see coverage in sonar or can folks provide any further recommendations?

Advertisement

Answer

Typically, the jacoco and surefire plugins have to be “integrated” properly, so that unit tests are run with the jacoco agent. I’m surprised that you get coverage if you don’t specify a surefire plugin. I imagine there must be defaults that make that happen. In my view, it’s best to be explicit, so it’s clear they are integrated.

You should probably have the following in your jacoco plugin:

                <execution>
                    <id>pre-unit-test</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <propertyName>surefireArgLine</propertyName>
                    </configuration>
                </execution>

And the following in surefire:

                <argLine>${surefireArgLine}</argLine>

If you really need those additional parameters, just add it to the end of this value.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement