本文介绍了运行gradle测试时找不到嵌套Kotlin类中的JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我按如下方式在Kotlin的嵌套类中指定测试时...

When I specify a test in a nested class in Kotlin as follows ...

import org.junit.jupiter.api.*

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

    @Test
    fun FoundTest() {}
}

...使用gradle运行测试时,JUnit无法识别它.仅找到并运行 FoundTest .

... it is not recognized by JUnit when running tests using gradle. Only FoundTest is found and ran.

我正在使用JUnit 5.1,Kotlin 1.2.30和Gradle 4.6.

I am using JUnit 5.1 and Kotlin 1.2.30 and 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() {}
}

为,默认情况下,Kotlin中的嵌套类类似于Java中的 static 类" JUnit文档指出:

在Kotlin中将类标记为 inner 会编译为非静态Java类.

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

这篇关于运行gradle测试时找不到嵌套Kotlin类中的JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 14:06
查看更多