Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sonar does not pick up Unit Tests Results even thought Code Coverage is generated

I am running SonarQuebe 6.2 on my local machine, I have Spring Boot Java 8 project with written unit tests that I want to upload to Sonar for static analysis all together with code coverage.
Code coverage is generated - I have my JaCoCo HTML report, JUnit XML test files are generated but my Sonar seems to miss Unit Tests result even thought that Code Coverage is diplayed:
pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.6.6</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.6.6</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.facebook4j</groupId>
        <artifactId>facebook4j-core</artifactId>
        <version>2.4.8</version>
    </dependency>

    <dependency>
        <groupId>net.sf.dozer</groupId>
        <artifactId>dozer</artifactId>
        <version>5.5.1</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-java8</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.4</version>
    </dependency>

</dependencies>

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.6.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.9</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>
                <execution>
                    <id>default-check</id>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    </execution>
                 <execution>
                    <id>generate-code-coverage-report</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>  

My sonar-project.properties:

sonar.projectKey=org.eventizer:EventizerServer
sonar.projectName=EventizerServer
sonar.projectVersion=1.0

sonar.log.level=DEBUG

sonar.sources=src/main/

sonar.language=java
sonar.java.source=1.8

sonar.sourceEncoding=UTF-8

sonar.java.binaries=target/classes/org/eventizer/eventizerserver/
sonar.java.test.binaries=target/test-classes/org/eventizer/eventizerserver/
sonar.tests=src/test/

sonar.java.coveragePlugin=jacoco
sonar.jacoco.reportPaths=target/jacoco.exec
sonar.junit.reportPaths=target/surefire-reports/  

I am running this mvn command:
mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent test -Dmaven.test.failure.ignore=true
As a result I am getting target directory with following output:
Target output directory

Classes directory that is set for sonar.java.binaries:
enter image description here

Test classes directory that is set for sonar.java.test.binaries:
enter image description here

Surefire JUnit test reports directory that is set for sonar.junit.reportPaths:
enter image description here

JaCoCo report output directory:
enter image description here

JaCoCo HTML report in browers:
enter image description here

After that I am running sonar-scanner.bat, below some important (I tihnk so) outputs:
enter image description here
enter image description here

My Sonar web instance project analysis: enter image description here

And I really do not have idea why this is happening since it looks like everything got generated properly. Since yesterday I think I have tried everything on StackOverflow so please do not mark it as duplicate.
This is even weirder because when I access Coverage metrics for this project I can see that 100% Unit tests passed:
enter image description here

like image 224
darekg11 Avatar asked Jul 15 '17 11:07

darekg11


People also ask

Does sonar do code coverage?

SonarQube and JaCoCo are two tools that we can use together to make it easy to measure code coverage. They also provide an overview of the overall health of the source code by finding code duplications, bugs and other issues in the code. This helps us to know whether our code is production-ready or not.

Does sonar run unit tests?

SonarQube doesn't run your tests or generate reports. It only imports pre-generated reports. Below you'll find language- and tool-specific analysis parameters for importing coverage and execution reports. In the Guides category of the SonarSource Community forum you might find instructions on generating these reports.

Does code coverage happen after unit testing?

Code coverage is calculated for a file based on the unit tests that SUCCESSFULLY passed. After all the unit tests are run you are able to see either in your browser the exact lines in your code that are being executed or right in your terminal.


2 Answers

Well... I suppose, that could because of sonar.sources=src/main/... If you set it as sonar.sources=src, it will show again.

And, I just find there's a Sonar Parater: sonar.tests=src/test will show the junit report in sonar.

here's my sonar-project.properties:

sonar.projectKey=com.test.marslo:marslo-test
sonar.projectName=marslo-test
sonar.projectVersion=1.1.0

sonar.projectBaseDir=.
sonar.sources=src/main
sonar.tests=src/test
sonar.java.binaries=build

sonar.sourceEncoding=UTF-8
sonar.java.source=1.8

sonar.jacoco.reportPaths=./build/jacoco/test.exec
sonar.junit.reportPaths=./build/test-results/test

And the build.gradle:

...
apply plugin: "jacoco"

...
...

jacoco {
    toolVersion = "0.8.0"
}

jacocoTestReport {
    reports {
        xml.enabled true
        csv.enabled true
        html.enabled true
    }
}

test {
    jacoco {
        append = false
        destinationFile = file("$buildDir/jacoco/jacocoTest.exec")
        classDumpDir = file("$buildDir/jacoco/classpathdumps")
    }
}

Build with:

gradle clean build test jacocoTestReport
like image 174
Marslo Avatar answered Oct 04 '22 20:10

Marslo


Okay, so I have discovered something that may be a Sonar bug.

Basically, this project has been pushed to Sonar for a long time with basic mvn sonar:sonar configuration. So, it was not even doing test results. Now I wanted to run that via Jenkins, so I filled all necessary fields in sonar-project.properties and pushed via Sonar-Runner not mvn sonar:sonar.

After doing so as you could see, Unit Tests Quality gate was failing with no good reason. Because in my latest screenshoot, you can see that in fact Unit Tests passed 100.0%.

I decide to push that analysis to a separate project by changing projectKey property to something else and all of the sudden everything went smoothly.

like image 24
darekg11 Avatar answered Oct 04 '22 21:10

darekg11