簡體   English   中英

有沒有一種簡便的方法可以在Spring Boot中使用Java注釋從測試范圍中排除類?

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

我有一個(gradle + kotlin)春季啟動項目,其中包含幾個DTO,配置類,常量等,我不想在測試覆蓋率分析期間進行分析。

我可以使用一種方便的Java表示法嗎?

您說您正在使用Kotlin和Gradle。 因此,我假設您正在使用jacoco進行測試。

這是jacoco涵蓋范圍之一,不包括示例。

jacocoTestReport {
    afterEvaluate {
        classDirectories = files(classDirectories.files.collect {
            fileTree(dir: it,
                    exclude: ['**/*Application**'])
        })
    }
}

這就是最終為我工作的原因。 不得不以一種非常古怪的方式編寫一些自定義的自定義過濾邏輯,但是它確實可以做到。

支持@Min Hyoung Hong的答案,將我引向正確的方向。

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一個例子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM