简体   繁体   English

有没有一种简便的方法可以在Spring Boot中使用Java注释从测试范围中排除类?

[英]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. 我有一个(gradle + kotlin)春季启动项目,其中包含几个DTO,配置类,常量等,我不想在测试覆盖率分析期间进行分析。

Is there a convenient Java notation that I can use? 我可以使用一种方便的Java表示法吗?

You said that you are using kotlin and gradle. 您说您正在使用Kotlin和Gradle。 So I assume that you are using jacoco for test coverage. 因此,我假设您正在使用jacoco进行测试。

This is one of the jacoco coverage excludes example. 这是jacoco涵盖范围之一,不包括示例。

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. 支持@Min Hyoung Hong的答案,将我引向正确的方向。

build.gradle.kts 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.

相关问题 如何使用 Jacoco 从覆盖分析中排除测试类 - How to exclude test classes form Coverage Analysis using Jacoco 从 Spring-Boot 测试中排除 elasticsearchTemplate - Exclude elasticsearchTemplate from Spring-Boot Test Java Spring Boot 测试:如何从测试上下文中排除 Java 配置类 - Java Spring Boot Test: How to exclude java configuration class from test context 构建 spring boot 应用程序时排除一些测试类 - Exclude some test Classes when building spring boot app 如何忽略来自数据 model 的测试覆盖率和来自 Java spring 引导项目的 dto - How to ignore test coverage from data model and dto from Java spring boot project 如何从代码覆盖率中排除某些类? (爪哇) - How to exclude certain classes from being included in the code coverage? (Java) spring-boot-devtools:如何排除java类重启应用程序? - spring-boot-devtools: how to exclude java classes from restarting the application? JaCoCo覆盖率报告设置(不包括测试班) - JaCoCo coverage report setups(exclude test classes) 需要一种为 Spring Boot 生成 Java 类的方法 - Need a way to generate Java classes for Spring Boot 从Spring引导单元测试中排除Spring Cloud Config Server - Exclude Spring Cloud Config Server from Spring boot unit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM