Skip to content

Document code or automatically configure Jacoco with Junit 5 Gradle Plugin #1024

Closed
@JLLeitschuh

Description

Feature (or Documentation) Request

Should the Junit 5 plugin automatically configure the junitPlatformTest task to use Jacoco if it detects the plugin?

Currently configuring jacoco with Junit 5 requires a bit of digging into the DSL and figuring out its intricacies.

Perhaps configure the Junit5 plugin should be expanded to have a enableJacoco flag that will add a jacocoJunit5TestReport task if set to true? What are the teams thoughts?

Current solutions

Anyone looking to configure jacoco with Junit 5 using the new Gradle Kotlin DSL you currently need to use a bit of a workaround because of a kotlin compiler bug. This code is known to work with Gradle 4.1

// Don't use this sample, see below
afterEvaluate {
    val junitPlatformTest by tasks
    jacoco {
        applyToHelper(junitPlatformTest)
    }
    task<JacocoReport>("jacocoJunit5TestReport") {
        executionData(junitPlatformTest)
        sourceSets(java.sourceSets["main"])
        sourceDirectories = files(java.sourceSets["main"].allSource.srcDirs)
        classDirectories = files(java.sourceSets["main"].output)
    }
}

/**
 * Workaround fix for calling [org.gradle.testing.jacoco.plugins.JacocoPluginExtension.applyTo]
 *
 * [Issue details here](/~https://github.com/gradle/kotlin-dsl/issues/458)
 */
fun org.gradle.testing.jacoco.plugins.JacocoPluginExtension.applyToHelper(task : Task) {
    val method = this::class.java.getMethod("applyTo", Task::class.java)
    method.invoke(this, task)
}

EDIT

Better solution with less code:

afterEvaluate {
    val junitPlatformTest : JavaExec by tasks
    jacoco {
        applyTo(junitPlatformTest)
    }
    task<JacocoReport>("jacocoJunit5TestReport") {
        executionData(junitPlatformTest)
        sourceSets(java.sourceSets["main"])
        sourceDirectories = files(java.sourceSets["main"].allSource.srcDirs)
        classDirectories = files(java.sourceSets["main"].output)
    }
}

Deliverables

  • Documentation about how to configure Jacoco with Gradle or add code to the plugin to do it automatically

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions