0

I can't reload my spring application after each test into my nested classes:

/*...*/
@DirtiesContext( classMode = AFTER_EACH_TEST_METHOD )
public class MyTestClass {

    @Nested
    class MyNestedClass_1 {

        @Test
        void test_1() {
            /*...*/
        }

        @Test
        void test_2() {
            /*...*/
        }
    }

    @Nested
    class MyNestedClass_2 {
        /*..*/
    }
}

Whereas, it works very well without nested class:

/*...*/
@DirtiesContext( classMode = AFTER_EACH_TEST_METHOD )
public class MyTestClass {

    @Test
    void test_1() {
        /*...*/
    }

    @Test
    void test_2() {
        /*...*/
    }
}

Does anybody have any idea why it doesn't work?

Zrom
  • 1,192
  • 11
  • 24

1 Answers1

1

The Spring support for JUnit 5's @Nested class feature is currently limited. You can follow the progress to resolve this on GitHub. For the time being, I guess you have to remove the nested classes and follow the progress closely.

You can also find a possible workaround at this question by adding @DirtiesContext( classMode = AFTER_EACH_TEST_METHOD ) on top of your nested class:

@DirtiesContext( classMode = AFTER_EACH_TEST_METHOD )
public class MyTestClass {

    @Nested
    @SpringBootTest
    @DirtiesContext( classMode = AFTER_EACH_TEST_METHOD )
    class MyNestedClass_1 {

        @Test
        void test_1() {
            /*...*/
        }

        @Test
        void test_2() {
            /*...*/
        }
    }

    @Nested
    @SpringBootTest
    @DirtiesContext( classMode = AFTER_EACH_TEST_METHOD )
    class MyNestedClass_2 {
        /*..*/
    }
}
rieckpil
  • 10,470
  • 3
  • 32
  • 56