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() {}
}
... it is not recognized by JUnit when running tests using gradle. Only FoundTest
is found and ran.
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() {}
}
As Sam Brannen indicates, "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.
Marking the class as inner
in Kotlin compiles to a non-static Java class.
From the documentation:
Only non-static nested classes (i.e. inner classes) can serve as
@Nested
test classes.
Thus, you need to make NestedTest
an inner
class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With