Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin test coverage not reported for Java production code with JaCoCo #530

Closed
realdadfish opened this issue Jan 29, 2024 · 8 comments · Fixed by #541
Closed

Kotlin test coverage not reported for Java production code with JaCoCo #530

realdadfish opened this issue Jan 29, 2024 · 8 comments · Fixed by #541
Assignees
Labels
Bug Bug issue type S: ready for release Status: merged in the main branch

Comments

@realdadfish
Copy link

realdadfish commented Jan 29, 2024

This is my configuration:

plugins {
    id("org.jetbrains.kotlinx.kover")
}

val configuration = extensions.create("glscoverage", GlsCoverageExtension::class.java)

afterEvaluate {
    kover {
        // not working
        useJacoco("0.8.11")
    }

    val expected = configuration.expectedCoverage.get()
    val threshold = configuration.coverageThreshold.get()
    val minBound = (expected - threshold).coerceAtLeast(0)
    val maxBound = (expected + threshold).coerceAtMost(100)

    koverReport {
        defaults {
            mergeWith(configuration.buildVariant.get())
        }

        filters {
            excludes {
                annotatedBy("*Generated*")
                classes(configuration.classFilter.get())
            }
        }

        verify {
            rule {
                minBound(minBound)
                maxBound(maxBound)
            }
        }

        androidReports(configuration.buildVariant.get()) {}
    }

    tasks.withType(Test::class.java).configureEach {
        finalizedBy(tasks.named("koverXmlReport"))
    }
}

Now I have separate Kotlin and Java source sets for my mixed-language module configured, i.e. src/{main,test}/{kotlin,java} and I have a Kotlin test that tests various Java source files. With the above configuration the coverage for these Java source files is reported as 0, even if I move the test from src/test/kotlin to src/test/java, however, if I comment out useJacoco above and let the IJ coverage processor do it's thing, the coverage of the classes in question is properly reported.

Environment

  • Kover Gradle Plugin version: 0.7.5
  • Gradle version: 8.5
  • Kotlin project type: Kotlin/Android
  • Coverage Toolset (if customized in build script): Kover, Jacoco (0.8.11)
@realdadfish realdadfish added Bug Bug issue type S: untriaged Status: issue reported but unprocessed labels Jan 29, 2024
@shanshin
Copy link
Collaborator

Hi,
try to move the kover { block from the afterEvaluate { to the top level, like this:

...
val configuration = extensions.create("glscoverage", GlsCoverageExtension::class.java)
kover {
    // not working
    useJacoco("0.8.11")
}

afterEvaluate {
    val expected = configuration.expectedCoverage.get()
    ...
}

@shanshin shanshin added S: waiting for clarification Status: additional information required to proceed and removed S: untriaged Status: issue reported but unprocessed labels Jan 29, 2024
@realdadfish
Copy link
Author

Didn't made a difference, same result. The coverage in Jacoco is still missing:
Bildschirmfoto 2024-01-29 um 18 24 15
Bildschirmfoto 2024-01-29 um 18 25 56

@shanshin
Copy link
Collaborator

Is this a multi-module project?

@realdadfish
Copy link
Author

The component in question has just one module, the app module. The convention plugin is applied on the app module level.

@shanshin
Copy link
Collaborator

Could you please provide a reproducer project?

@realdadfish
Copy link
Author

Here it is: https://github.com/realdadfish/kover-issue-530/tree/main

It seems to be triggered by Robolectric on the test classpath. If I have two regular Java/Kotlin unit tests, both execute fine and the coverage shows that both methods are covered. If I let each test run with the Robolectric runner, neither test shows coverage on the class with JaCoCo, but both tests show coverage with the built-in coverage tool

@shanshin shanshin added S: confirmed Status: bug is reproduced or present and removed S: waiting for clarification Status: additional information required to proceed labels Jan 29, 2024
shanshin added a commit that referenced this issue Feb 14, 2024
… reports

It seems that after Robolectric instrumentation, source locations are lost and JaCoCo does not instrument such classes by default. There is a need to change the flag to disable this behavior.

After changing this flag, errors occur in the instrumentation of JVM classes (because they were previously excluded due to the lack of sources). The package with these classes was determined empirically.

Fixes #530
shanshin added a commit that referenced this issue Feb 15, 2024
… reports

It seems that after Robolectric instrumentation, source locations are lost and JaCoCo does not instrument such classes by default. There is a need to change the flag to disable this behavior.

After changing this flag, errors occur in the instrumentation of JVM classes (because they were previously excluded due to the lack of sources). The package with these classes was determined empirically.

Fixes #530
PR #541
@shanshin shanshin reopened this Feb 15, 2024
@shanshin shanshin added S: ready for release Status: merged in the main branch and removed S: confirmed Status: bug is reproduced or present labels Feb 15, 2024
@realdadfish
Copy link
Author

Many thanks!

@shanshin
Copy link
Collaborator

Fixed in 0.7.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Bug issue type S: ready for release Status: merged in the main branch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants