如何在 kotlin 中编写 @Nested 测试 类

How to write @Nested test classes in kotlin

编写 junit 测试时:

internal class MyTest {
    @org.junit.jupiter.api.Nested
    class MyInnerClass {

    }
}

我的代码编辑器 (IntelliJ) 显示警告

Only non-static nested classes can serve as @Nested test classes.

如何在 kotlin 的 junit 测试中编写嵌套 类?

在 kotlin 中使用 @Nested 类 时,它们需要前缀 inner,因为只有内部 类 可以用作嵌套 类。

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

您的代码应如下所示:

internal class MyTest {
    @Nested
    inner class MyInnerClass {
        @Test
        fun customTest() {
           //TODO: do something
        }
    }
}