my sonarqube always said no code coverage for java - java

hi all i have build unit testing for controller, repository and Service but why in my sonarqube code coverage always give me code coverage only 0 percent. my question is how to make my percentage up ?
here what i was code in my testing

Sonarqube uses existing code coverage reports from JaCoCo (in the case of Java). Usually, you would set up the JaCoCo Maven plugin (or Gradle) to gather coverage info on test run and Sonarqube then loads this report.
See also the Sonarqube docs for info on setup. But if you are using Maven/Gradle, I believe Sonarqube is able to automatically pick up the correct file unless you have some special configuration.

Related

I am using Gauge framework with Java and maven and wanted to implement Test coverage using Jacoco. At spec level,

For example if 10 specs are run. then code coverage report should be generated for the same. I have my classes in src/test/Java folder.
Is there any way to achieve this?
I have tried using antrun plugin. It's getting executed but code coverage for all the classes is displayed as zeros. however execution of specs are successful and it's pass

Cannot use Jacoco in IntelliJ IDEA

In my Java app, in order to generate coverage reports for my Unit Tests, I have tried several approaches to setup Jacoco as in Run with coverage or this page. However, I cannot generate coverage reports as expected and I think I need some steps describing the setup of Jacoco on IntelliJ. So, does anybody else have experience for setup and run it via command line as shown below:
mvn --global-settings ../settings.xml
-Dtest="/com/mycompany/core/service/impl/unit/ProductServiceImplTest.java" test
Any help would be appreciated.

Does Spring boot provide any internal library for generating coverage report

I am working on an assignment in which I need to generate code coverage report without using any plugin.I have gone through the Spring boot documentation but could not find any article on coverage report generation. Is it possible to generate coverage report without using any plugin like jacoco or maven-surefire-report-plugin or sonarQube. Any input will be highly appreciated. Thanks!!
No, It isn't. Spring Boot's maven/gradle plugins does not provide support for code coverage out of the box, you will need to use either JaCoCo or a similar tool.
See:
Generate Code Coverage with JaCoCo and spring-boot-maven-plugin
Code Coverage and Source Quality Analysis with Spring Boot + Docker + SonarQube + JaCoCo

How to turn off code coverage in SonarQube 4?

How to turn off code coverage in SonarQube 4? I have JMockit usage in unit tests and JaCoCo code coverage plugin in Sonar. They conflict because they use different javaagents to edit bytecode of classes (as I know). I want to switch off code coverage in Sonar. I can exclude my project in settings of Jacoco, but it doesn't help.
I recommend following the paragraph "Ignore Code Coverage" in this documentation:
http://docs.sonarqube.org/display/SONAR/Narrowing+the+Focus

Sonar Java: check the quality of the test classes source code?

Is it possible to check in Sonar the quality of the *Test.java source code, e.g. Methods maximum size 100 lines?
The problem is, that the Java Junit tests are growing with the productive code, also the complexity.
We have unit test classes with more than 1000 lines and 2 methods.
We want to check in Sonar some rules for these *Test.java classes.
Since Sonar 3.1, it includes a plugin that has specific PMD rules to be executed against the unit tests (a JIRA was created for that). You can see them in the Configuration > Quality Profiles > Coding Rules.
However, it seems that you want to run a full analysis on the test source code, like you do on the production source code, and get additional metrics (for ex. a % rules compliance and also a % rules compliance for unit tests). I don't think that Sonar provides such feature natively. What you can do is to run 2 Sonar analysis:
Your first analysis is the current one;
The second analysis will consider the src/test/java as the "production" source code. Thus, this second analysis will give you the quality of your code. For this analysis, you can specify a specific Maven profile (or an alternative pom.xml) that will change the project information (for ex. it will indicate that src/test/java is the default sourceDirectory).
I also noticed that SonarQube will by default ignore the test resources for quality analysis. Using schnatterers answer, i found a simple way to create a separate project only including the test classes as sources in SonarQube, therefore triggering the quality anlysis on them. In the POM of the project i want to analyze i add a profile, which changes the sonar properties accordingly:
<profiles>
<profile>
<id>analyze-test-classes</id>
<properties>
<sonar.sources>src/test/java</sonar.sources>
<sonar.tests></sonar.tests>
<sonar.projectName>${project.name}-tests</sonar.projectName>
<sonar.projectKey>${project.groupId}:${project.artifactId}-tests</sonar.projectKey>
</properties>
</profile>
</profiles>
Running Maven with
mvn sonar:sonar -Panalyze-test-classes
will then activate this profile and create an additional project in SonarQube with the suffix -tests, which only contains the analysis of the test classes.
With SonarQube 4.5.2 (don't know when they changed the behavior) it seems to me that unit tests are no longer excluded from the analysis. When running sonar-runner with sonar.sources=src sonar also creates issues for src/test/java.
One approach to use a specific quality ruleset for test code would be to run two analyses: one for the main code and another one for the testing code.
This can be realized as follows:
sonar-project.properties:
sonar.projectName=testSonar
sonar.projectKey=testsonar
sonar.sources=src/main/java
sonar.projectVersion=1.0
Analyse main code: sonar-runner
Analyse test code: sonar-runner -Dsonar.projectKey=testsonar.test -Dsonar.sources=src/test/java -Dsonar.projectName="testSonar TEST"
The different quality profiles must be changed via the server (Dashboard | Project Configuration | Quality Profiles), because -Dsonar.profile is deprecated.
This should also work with analyses through maven or jenkins.

Categories

Resources