2

I have a (gradle + kotlin) spring boot project with several DTOs, configuration classes, constants etc. that I don't want to be analyzed during test coverage analysis.

Is there a convenient Java notation that I can use?

Clement
  • 4,491
  • 4
  • 39
  • 69

2 Answers2

2

You said that you are using kotlin and gradle. So I assume that you are using jacoco for test coverage.

This is one of the jacoco coverage excludes example.

jacocoTestReport {
    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it,
                    exclude: ['**/*Application**'])
        })
    }
}
Min Hyoung Hong
  • 1,102
  • 9
  • 13
  • Hey bro this didn't quite do it for me, but it did lead me down the right track so I'm upvoting your answer :) – Clement Nov 28 '18 at 12:11
0

This is what ended up working for me. Had to write some custom custom filtering logic in a very hacky sort of way, but it did the job.

Upvoting @Min Hyoung Hong's answer for leading me down the right track.

build.gradle.kts

tasks {
    withType<KotlinCompile<KotlinJvmOptions>> {
        kotlinOptions.freeCompilerArgs = listOf("-Xjsr305=strict")
        kotlinOptions.jvmTarget = "1.8"
    }

    withType<JacocoReport> {
        reports {
            xml.isEnabled = false
            csv.isEnabled = false
            html.destination = file("$buildDir/jacocoHtml")
        }

        afterEvaluate {
            val filesToAvoidForCoverage = listOf(
                    "/dto",
                    "/config",
                    "MyApplicationKt.class"
            )
            val filesToCover = mutableListOf<String>()
            File("build/classes/kotlin/main/app/example/core/")
                    .walkTopDown()
                    .mapNotNull { file ->
                        var match = false
                        filesToAvoidForCoverage.forEach {
                            if (file.absolutePath.contains(it)) {
                                match = true
                            }
                        }
                        return@mapNotNull if (!match) {
                            file.absolutePath
                        } else {
                            null
                        }
                    }
                    .filter { it.contains(".class") }
                    .toCollection(filesToCover)

            classDirectories = files(filesToCover)
        }
    }
}
Clement
  • 4,491
  • 4
  • 39
  • 69