简体   繁体   中英

Is there an easy way to exclude classes from test coverage using Java Annotations in Spring Boot?

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?

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**'])
        })
    }
}

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)
        }
    }
}

我不KHOW你用什么样的工具来覆盖你的测试类,但在这里是如何从测试覆盖率排除类与jacoco一个例子。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM