运行 gradle 测试时未找到嵌套 Kotlin class 中的 JUnit 测试
JUnit test in nested Kotlin class not found when running gradle test
当我在 Kotlin 的嵌套 class 中指定测试时,如下所示...
import org.junit.jupiter.api.*
class ParentTest
{
@Nested
class NestedTest
{
@Test
fun NotFoundTest() {}
}
@Test
fun FoundTest() {}
}
...当 运行 使用 gradle 测试时,JUnit 无法识别它。仅找到 FoundTest
和 运行.
我正在使用 JUnit 5.1 和 Kotlin 1.2.30 以及 Gradle 4.6。
将嵌套 class 定义为 an inner class 可解决此问题。
class ParentTest
{
@Nested
inner class NestedTest
{
@Test
fun InnerTestFound() {}
}
@Test
fun FoundTest() {}
}
如, "by default, a nested class in Kotlin is similar to a static
class in Java" and the JUnit documentation indicates:
Only non-static nested classes (i.e. inner classes) can serve as
@Nested test classes.
在 Kotlin 中将 class 标记为 inner
编译为 non-static Java class.
Only non-static nested classes (i.e. inner classes) can serve as @Nested
test classes.
因此,您需要将 NestedTest
设为 inner
class。
当我在 Kotlin 的嵌套 class 中指定测试时,如下所示...
import org.junit.jupiter.api.*
class ParentTest
{
@Nested
class NestedTest
{
@Test
fun NotFoundTest() {}
}
@Test
fun FoundTest() {}
}
...当 运行 使用 gradle 测试时,JUnit 无法识别它。仅找到 FoundTest
和 运行.
我正在使用 JUnit 5.1 和 Kotlin 1.2.30 以及 Gradle 4.6。
将嵌套 class 定义为 an inner class 可解决此问题。
class ParentTest
{
@Nested
inner class NestedTest
{
@Test
fun InnerTestFound() {}
}
@Test
fun FoundTest() {}
}
如static
class in Java" and the JUnit documentation indicates:
Only non-static nested classes (i.e. inner classes) can serve as @Nested test classes.
在 Kotlin 中将 class 标记为 inner
编译为 non-static Java class.
Only non-static nested classes (i.e. inner classes) can serve as
@Nested
test classes.
因此,您需要将 NestedTest
设为 inner
class。