运行gradle测试时找不到嵌套Kotlin类中的JUnit测试 [英] JUnit test in nested Kotlin class not found when running gradle test

查看:102
本文介绍了运行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() {}
}

仅非静态嵌套类(即内部类)可以用作@嵌套测试类.

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

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆