Capturing logging entries on code coverage - java

I'm using Maven Surefire plugin to run my project's unit tests and Jacoco for code coverage. I have a strict coverage criteria in my project but my problem is that logging level specific lines are not captured by the Jacoco and messing up with my covering stats.
For example:
if (log.isDebugEnabled()) {
log.debug("Log entry");
}
Will fail to identify the log entry code line. Any way to work around it?

Related

Jacoco - How to check which lines are missing coverage

I am using jacoco for Test coverage verification in a Java Library created with Gradle. I have set the verification rule to limit minimum coverage to 1.0 (100%). I have a few classes in my code which are not meeting the criteria. based on the unit-tests that I have written, I don't think I am missing any line of code.
on those lines, I was wondering if there is a was to make jacoco print out which lines are not getting covered by unit tests during the build process?
currently jacoco causes a build failure reporting the coverage ration for each files that didn't meet the bar but it does not provide any information on specific lines that were not covered.
I was able to configure My Gradle-Groovy project to start generating Jacoco coverage reports by doing the following:
added jacoco plugin to buildscript in build.gradle
apply plugin: 'jacoco'
adding a custom configuration:
jacocoTestReport {
dependsOn test
}
chaining jacocoTestReport to your test task
test {
finalizedBy jacocoTestReport
}
This ensures, report is always generated after tests run.
Note: if you have added both jacoco and java plugin, reports are automatically generated and stored at $buildDir/reports/jacoco/test in such a scenario, you can skip above steps.

SonarQube 6.7.7 How to add junit test count to dashboard without analysing tests

When I add sonar.tests I get the tests analysed and the number of tests, I only want the number of tests but not the analysis.
sonar-project.properties:
sonar.projectKey=xxx
sonar.java.binaries=target/classes
sonar.java.test.binaries=target/test-classes
sonar.sources=src/main/java
sonar.tests=src/test/java
sonar.test.exclusions=src/test/java/**
sonar.java.source=11
sonar.sourceEncoding=UTF-8
sonar.coverage.jacoco.xmlReportPaths=target/site/jacoco/jacoco.xml
sonar.dynamicAnalysis=reuseReports
sonar.junit.reportPaths=target/surefire-reports/testng-junit-results/junitreports/
You cannot do it. It is impossible.

Code Coverage with SonarQube integrated with Tomcat without Maven

I ran code coverage with jacoco(using javaagent)integrating it in startup.bat of tomcat and got jacoco.exec. I also got a html report for that.
Now, I want code coverage in sonarqube. I ran sonar-scanner and got all the details except "Code-coverage".
Is there a way to have Code-Coverage without updating the pom.xml? Or if I could have code-coverage in sonarqube using jacoco.exec?
It is possible. You just have to add sonar.jacoco.reportPaths parameter.
Examples:
SonarQube Scanner + sonar-project.properties:
sonar.jacoco.reportPaths=/path/jacoco.exec,/path/another/jacoco.exec
Gradle:
sonarqube {
properties {
property "sonar.jacoco.reportPaths", "/path/jacoco.exec,/path/another/jacoco.exec"
}
}
Maven:
mvn sonar:sonar -Dsonar.jacoco.reportPaths=/path/jacoco.exec,/path/another/jacoco.exec
Read more here: Java Unit Tests and Coverage Results Import

Generate Jacoco code coverage report for individual unit tests using gradle

Gradle 'Jacoco' plugin's 'jacocoTestReport' generates code coverage report for all the unit tests.
How should I proceed to create a gradle task to generate 'Jacoco' code coverage reports of each unit test individually?
HelloWorld.java
HelloWorldTest.java contains Test1, Test2, Test3 methods
I want to generate individual Jacoco code coverage report for Test1, Test2, Test3 methods.
During running of the tests, Jacoco instruments the classes and records what was called. The task of type JacocoReport then just takes these results and makes an XML, HTML or CSV report out of them.
So to get what you want, you need to add several Test tasks that execute the single tests and then add several JacocoReport tasks that point to the different result files, then you can generate these reports in one run.
If you only want to do this manually, I think you can just call Gradle like gradlew test --tests HelloWorld.Test1 jacocoTestReport.
In the latter case it might be necessary to also set test { jacoco { append false } } to not have the results of former runs in the report.

Gradle jacoco multi project. How to output the code coverage of all the project in the terminal?

I am using Gradle jacoco plugin and have JUnit tests in a gradle multi project.
I have installed jacoco in order to report to sonarqube my code coverage, which I did.
Gitlab-CI also offer a badge for the code coverage, I only need to output the code coverage in a terminal and use a regex to find it.
Is there a way to output the code coverage of all the project in the terminal ?
Yes, you can. Possible ways:
1) Use Gradle plugin gradle-console-reporter to report various kinds of summaries to console. JUnit, JaCoCo and Cobertura reports are supported.
In your case, following output will be printed to console:
...
BUILD SUCCESSFUL
Total time: 4.912 secs
Coverage summary:
project1: 72.2%
project2-with-long-name: 44.4%
Then you can use coverage with regular expression in Gitlab's .gitlab-ci.yml to parse code coverage.
2) Second option is a little bit tricky. You can print full JaCoCo HTML report (e.g. using cat target/site/jacoco/index.html) and then use regular expression (see this post) or grep (see this post) to parse coverage.
AFAIK Gradle does not support this, each project is treated separately.
To support your use case some aggregation task can be created to parse a report and to update some value at root project and finally print that value to stdout.
Update with approximate code for solution:
subprojects {
task aggregateCoverage {
// parse report from each module into ext.currentCoverage
rootProject.ext.coverage += currentCoverage
}
}

Categories

Resources