0

I have a project build based on Gradle. Now I added a lot of Unit Tests to it, and trying to integrate it into Jenkins to run them while building the project, and generate Code Coverage report (this is where JaCoCo comes into play).

I have the following config in build.gradle:

apply plugin: 'java'
apply plugin: 'application'
apply plugin: "jacoco"

buildDir "out"

repositories {
    maven {
        url "https://internal.corporate.repo.com/"
    }

    flatDir{
        dirs 'resources/lib'
    }

}

sourceSets {
    main {
        java {
            srcDir 'src/'
        }
        resources {
            srcDir 'resources/'
        }
    }
}

jacoco {
    toolVersion = "0.7.5.201505241946"
}

dependencies {
    compile 'org.apache.ant:ant-launcher:1.9.7'
    compile 'org.jdom:jdom:2.0.2'
    compile 'joda-time:joda-time:2.9.3'
    compile 'com.sun.mail:javax.mail:1.5.5'
    compile 'org.json:json:20160212'
    compile 'org.apache.wink:wink-client:1.4'
    compile 'org.apache.wink:wink-common:1.4'
    compile 'org.apache.wink:wink-json-provider:1.4'
    compile 'javax.ws.rs:javax.ws.rs-api:2.0.1' 
    compile 'org.apache.commons:commons-lang3:3.4'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'org.apache.ant:ant:1.9.7'
    compile 'org.slf4j:slf4j-api:1.7.25'
    compile 'org.apache.logging.log4j:log4j-api:2.9.0'
    compile 'org.apache.logging.log4j:log4j-core:2.9.0'
    compile 'org.apache.logging.log4j:log4j-slf4j-impl:2.9.1'
    compile 'javax.activation:activation:1.1.1'
    compile 'com.opencsv:opencsv:3.7'
    compile 'junit:junit:4+'
    compile name:'ant-soatest'  
    compile name:'ant-junit'
    compile 'commons-cli:commons-cli:1.3.1'
    compile 'org.apache.ant:ant-junit4:1.9.7'
    compile 'commons-io:commons-io:2.5'
    compile 'com.oracle.jdbc:ojdbc7:12.1.0.2'

    compile 'org.apache.commons:commons-configuration2:2.1.1'
    compile 'commons-beanutils:commons-beanutils:1.9.3'
    compile 'com.sun.jersey:jersey-client:1.8'
    compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.8.5'
    compile 'jaxen:jaxen:1.1.6'

}

 jacocoTestReport {
    reports {
        xml.enabled true
        csv.enabled true
        html.destination file("${buildDir}/jacocoHtml")
    }
}

test {
    jacoco {
        excludes = ['**/*.class']
    }
    testLogging.showStandardStreams = true 
 }

So I have two problems:

  1. When running the tests task, the outputted test.exec shows 0% coverage while I import it to Coverage tab in Eclipse. Even though I know tests were run, the Gradle testing result shows 3 successful tests, this is the reason I moved on to the second attempt to actually get coverage results
  2. jacocoTestReport task - this one ends up with the following errors currently:

14:18:03.517 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:46) 14:18:03.517 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Caused by: : Error while creating report

followed by:

14:18:03.520 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Caused by: java.io.IOException: Error while analyzing class /bld/workspace/some/DEV/really/long/obscure/pathname/appname/out/resources/main/testscripts/Cucumber/target/RenamedCommonDependencies.jar@oracle/jdbc/proxy/oracle$1jdbc$1replay$1driver$1NonTxnReplayableArray$2java$1sql$1Array$$$Proxy.class.

and finally followed by:

Caused by: java.lang.IllegalStateException: Can't add different class with same name: oracle/jdbc/proxy/oracle$1jdbc$1replay$1driver$1NonTxnReplayableArray$2java$1sql$1Array$$$Proxy 14:18:03.521 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] at org.jacoco.core.analysis.CoverageBuilder.visitCoverage(CoverageBuilder.java:106)

I have tried everything I could think of, last looking at Exclude package from jacoco code coverage for Android and JaCoCo gradle plugin exclude I tried to change the excludes path to absolute using /bld/workspace/some/DEV/really/long/obscure/pathname/appname/out/resources/main/testscripts/Cucumber/target/RenamedCommonDependencies.jar but same error

PLEASE help.. I am wasting 99% of my development time dealing with configuration, only 1% writing code... :(

Carmageddon
  • 2,627
  • 4
  • 36
  • 56

1 Answers1

0

I think that the task setup is not correct shold be something like

apply plugin: "application"
apply plugin: "jacoco"

mainClassName = "org.gradle.MyMain"

jacoco {
    applyTo run
}

task applicationCodeCoverageReport(type:JacocoReport){
    executionData run
    sourceSets sourceSets.main
}

Where in main class you put your class name, cause you don't need code coverage for all the system libs.. You can see an Example here: https://github.com/codecov/example-gradle

Example and info can be found here: https://docs.gradle.org/current/userguide/jacoco_plugin.html#sec:configuring_the_jacoco_plugin

However, I think that you had bet on the wrong tool ... Check out http://pitest.org/

OBender
  • 2,492
  • 2
  • 20
  • 33