5

When writing a junit test:

internal class MyTest {
    @org.junit.jupiter.api.Nested
    class MyInnerClass {

    }
}

A warning is displayed by my code editor (IntelliJ)

Only non-static nested classes can serve as @Nested test classes.

How to write a nested classes in junit tests in kotlin?

jactor-rises
  • 2,395
  • 2
  • 22
  • 44
  • 1
    https://stackoverflow.com/questions/49180845/junit-test-in-nested-kotlin-class-not-found-when-running-gradle-test – Karthik Kompelli Feb 13 '21 at 09:56
  • Does this answer your question? [JUnit test in nested Kotlin class not found when running gradle test](https://stackoverflow.com/questions/49180845/junit-test-in-nested-kotlin-class-not-found-when-running-gradle-test) – ggorlen Feb 25 '22 at 03:42

1 Answers1

7

When using @Nested classes in kotlin they need the prefix inner as only inner classes can be used as nested classes.

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

Your code should look somewhat like this:

internal class MyTest {
    @Nested
    inner class MyInnerClass {
        @Test
        fun customTest() {
           //TODO: do something
        }
    }
}
Winter
  • 126
  • 1
  • 5