I'm configuring Jacoco(version 0.8.3) unit test reporting for specific module in Gradle(version 6.0.1) project. Currently, I have basic java project structure, where I have in given module src and src/main, src/test repos with core code files and unit tests.
This is my configuration in specific build.gradle file for given module:
apply plugin: 'java'
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.8.3"
}
jacocoTestReport {
reports {
xml.enabled true
html.enabled true
}
}
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
counter = 'LINE'
value = 'COVEREDRATIO'
value = 'COVEREDRATIO'
minimum = 1.0
}
}
}
}
sonarqube {
properties {
property "sonar.coverage.jacoco.xmlReportPaths", "build/customJacocoReportDir/test/jacocoTestReport.xml"
property "sonar.java.binaries", "build/classes/java/main"
property "sonar.java.test.binaries", "build/classes/java/test"
property "sonar.sources", "src/main"
property "sonar.tests", "src/test"
}
}
However, after launching jacocoTestReport, xml file with test results is generated but shows 0 unit test coverage, even when I have unit tests covering my classes. Unit test classes are also included in generated index.html file.
This project has standalone gradle configuration for unit tests, I think problem may be there, I wasn't able to locate it.
junit-test.gradle file:
sourceSets.test.output.resourcesDir = file('build/classes/java/test/')
configurations {
testCompile {
exclude group: 'junit'
exclude group: 'org.aspectj'
exclude group: 'org.hamcrest'
exclude module: 'jsonassert'
exclude module: 'jsr305'
}
}
dependencies {
testImplementation(
// Junit 5
'org.junit.jupiter:junit-jupiter-api',
'org.junit.jupiter:junit-jupiter-params',
)
testRuntimeOnly(
'org.junit.jupiter:junit-jupiter-engine',
)
}
tasks.named('compileTestJava').configure {
options.encoding = 'UTF-8'
doFirst {
moduleOptions {
addReads = [(project.moduleName): 'org.junit.jupiter.api']
}
}
}
tasks.named('test').configure { Test test ->
test.useJUnitPlatform {
includeEngines 'junit-jupiter'
// scanForTestClasses false
include "***/**/*Tests.class"
include "****/**/*TestFactory.class"
filter {
failOnNoMatchingTests false
}
}
test.testLogging {
events "passed", "skipped", "failed"
exceptionFormat TestExceptionFormat.FULL
}
test.reports {
html.enabled = true
}
doFirst {
moduleOptions {
addReads = [(project.moduleName): 'org.junit.jupiter.api']
addOpens = ['java.base/java.lang': 'spring.core']
}
def testJvmArgs = [
'-ea',
'-Dfile.encoding=utf-8',
'-Dsun.jnu.encoding=utf-8',
'-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager',
'-Duser.country=**',
'-Duser.language=**',
'-Duser.timezone=***,
]
configurations.agent.files.each {
testJvmArgs.add 1, "-javaagent:$it.path"
}
test.jvmArgs testJvmArgs
}
}
Thank you for your help.