Testkit Jacoco Coverage


(Lance Java) #1

I’m using the approach mentioned here to generate a gradle.properties file which I use in my testKit test

eg:

configurations {
	jacocoRuntime
}
dependencies {
	jacocoRuntime "org.jacoco:org.jacoco.agent:${jacoco.toolVersion}:runtime"
}
task createTestkitFiles {
	def outputDir = file("$buildDir/testkitFiles")

	inputs.files configurations.jacocoRuntime
	outputs.dir outputDir

	doLast {
		outputDir.mkdirs()
		file("$outputDir/testkit-gradle.properties").text = "org.gradle.jvmargs=-javaagent:${configurations.jacocoRuntime.asPath}=destfile=$buildDir/jacoco/test.exec"
	}
}

test.dependsOn createTestkitFiles

The contents of testkit-gradle.properties contains

org.gradle.jvmargs=-javaagent:C:\Users\Lance\.gradle\caches\modules-2\files-2.1\org.jacoco\org.jacoco.agent\0.7.6.201602180812\4205019e7f7cd429b1b35090cd14aa6c8444ba31\org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=C:\code\gradle-maven-share\plugin\build/jacoco/test.exec

When I try to copy this to gradle.properties in a testkit test I get the following

    java.lang.IllegalStateException: An error occurred executing build with args ':project1:dependencies --stacktrace' in directory 'C:\Users\Lance\AppData\Local\Temp\junit6007038992566519310'
        at org.gradle.testkit.runner.internal.ToolingApiGradleExecutor.run(ToolingApiGradleExecutor.java:124)
        at org.gradle.testkit.runner.internal.DefaultGradleRunner.run(DefaultGradleRunner.java:270)
        at org.gradle.testkit.runner.internal.DefaultGradleRunner.build(DefaultGradleRunner.java:219)
        at com.lazan.gradlemavenshare.MavenSharePluginTest.Maven dependency is shared with gradle(MavenSharePluginTest.groovy:85)

        Caused by:
        org.gradle.tooling.GradleConnectionException: Could not execute build using Gradle installation 'C:\Applications\gradle-2.13'.
            at org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:55)
            at org.gradle.tooling.internal.consumer.ExceptionTransformer.transform(ExceptionTransformer.java:29)
            at org.gradle.tooling.internal.consumer.ResultHandlerAdapter.onFailure(ResultHandlerAdapter.java:41)
            at org.gradle.tooling.internal.consumer.async.DefaultAsyncConsumerActionExecutor$1$1.run(DefaultAsyncConsumerActionExecutor.java:57)
            at org.gradle.testkit.jarjar.org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
            at org.gradle.testkit.jarjar.org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
            at java.lang.Thread.run(Thread.java:745)
            at org.gradle.tooling.internal.consumer.BlockingResultHandler.getResult(BlockingResultHandler.java:46)
            at org.gradle.tooling.internal.consumer.DefaultBuildLauncher.run(DefaultBuildLauncher.java:77)
            at org.gradle.testkit.runner.internal.ToolingApiGradleExecutor.run(ToolingApiGradleExecutor.java:100)
            ... 3 more

            Caused by:
            org.gradle.api.GradleException: Unable to start the daemon process.
            This problem might be caused by incorrect configuration of the daemon.
            For example, an unrecognized jvm option is used.
            Please refer to the user guide chapter on the daemon at https://docs.gradle.org/2.13/userguide/gradle_daemon.html
            Please read the following process output to find out more:
            -----------------------
            Error occurred during initialization of VM
            agent library failed to init: instrument
            Error opening zip file or JAR manifest missing : C:UsersLance.gradlecachesmodules-2


Setting up Jacoco using gradle to calculate java code coverage during System Testing
Gradle plugins integration tests code coverage with Jacoco plugin
Bug in Jacoco Plugin
(Lance Java) #2

I think just writing my question in the forum was enough to see the problem.
It looks like a windows backslash issue, the following fixed it

String jacocoPath = configurations.jacocoRuntime.asPath.replace('\\', '/')
file("$outputDir/testkit-gradle.properties").text = "org.gradle.jvmargs=-javaagent:${jacocoPath}=destfile=$buildDir/jacoco/test.exec"


(Philip Cheong) #3

Are you using the GradleRunner? If so, how did you get it to pick up the file $outputDir/testkit-gradle.properties?


(Lance Java) #4

Yes, I got it all working in the end… see the following lines

Here I create testkit-gradle.properties

And here I copy testkit-gradle.properties to gradle.properties in the GradelRunner directory


(Philip Cheong) #5

Ah great @Lance_Java! That’s just what I was looking for! Thanks!

Edit:
Just for the record, the answer to my question is this line: