当我在Kotlin中的嵌套类中指定测试时,如下所示...

import org.junit.jupiter.api.*

class ParentTest
{
    @Nested
    class NestedTest
    {
        @Test
        fun NotFoundTest() {}
    }

    @Test
    fun FoundTest() {}
}

... JUnit在使用gradle运行测试时无法识别它.只有FoundTest人被找到并跑掉了.

我使用的是JUnit 5.1、Kotlin 1.2.30和Gradle 4.6.

推荐答案

Defining the nested class as an inner class resolves this issue.

class ParentTest
{
    @Nested
    inner class NestedTest
    {
        @Test
        fun InnerTestFound() {}
    }

    @Test
    fun FoundTest() {}
}

As Sam Brannen indicates"by default, a nested class in Kotlin is similar to a 102 class in Java"the JUnit documentation indicates:

Only non-static nested classes (i.e. inner classes) can serve as @Nested test classes.

Marking the class as inner in Kotlin compiles to a non-static Java class.

Kotlin相关问答推荐

Kotlin Poet 的导入不明确

如何优雅地声明一个StateFlow?

Kotlin中反射中Int到可空Double的隐式转换失败

如何在 Spring Boot 3 中为内部类提供运行时提示

Jetpack compose 可滚动表格

compareBy 如何使用布尔表达式在 kotlin 中工作

如何为你的 Flutter 元素添加 Kotlin 支持?

Swift vs Kotlin 在排序数组上的表现

Fragment的onDestroy()中是否需要将ViewBinding设置为null?

为什么 android studio 不为所有安全参数生成代码?

Kotlin 中的 Java Scanner 相当于什么?

如何在主线程上使用 Kotlin 协程 await()

如何在 kotlin 中生成 json 对象?

在 Koin 中提供一个 Instance 作为其接口

如何在 Spring WebFlux 的响应正文中流式传输二进制数据

Android Studio 和 Kotlin:Unresolved reference: also

Ktor 在 java.library.path 中没有 netty_transport_native_epoll_x86_64

ObjectAnimator.ofFloat 不能直接在kotlin中取Int作为参数

如何从协程范围返回值

Kotlin var lazy init