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

标签 kotlin junit5

编写 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
        }
    }
}

关于kotlin - 如何在 kotlin 中编写@Nested 测试类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66135286/

相关文章:

java - 使用接口(interface)反序列化 Kotlin 类时出现 InvalidDefinitionException

java - 如何在junit 5中为测试用例设置环境变量

eclipse - 无法在Jipster示例应用程序Gradle中运行Eclipse中的Junits

spring - JUnit 5 中 Autowiring 的 applicationContext 为 null

java - 使用 JPA Criteria API 进行分页的总行数

android - 如何统计 Kotlin Android 中的字数?

java - JUnit5:无法找到 AssertEquals

java - JUnit 断言抛出中的可执行文件会发生什么情况?

android - 重新将服务附加到 Activity ?

android - ViewModel和LiveData Observer不调用