简体   繁体   中英

How to write inner test classes if you use Spring and Junit 4? Or How else I can structure my tests?

I have a test class which in the future will test different conditions for tests and I'd like to make it more structured and fit only into one file. I found this solution but it doesn't suit me because I can't combine a SpringRunner.class and Enclosed.class to fit it into one @RunWith . I can't use Nitor Creation's Nested Runner - it brings the same problem.

I use Spring 5 and Junit 4.12 . Well... my question is, how to combine my tests inside a few inner/nested classes with one root class?

UPD: one remark - I can't make an upgrade to Junit 5 .

JUnit 4

If you need to stick with JUnit 4, you can use a third-party plugin to provide support. See the junit-hierarchicalcontextrunner provided by bechte on GitHub.

Add the dependency to your project, and use the HeirachalContextRunner like so:

@RunWith(HierarchicalContextRunner.class)
public class NestedTest {

    @ClassRule
    public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

    @Rule
    public final SpringMethodRule  springMethodRule = new SpringMethodRule();

    @Before
    public void setup() {
        // General test-suite setup
    }

    public class NestedClass {

        @Test
        public void testSomething() {
            // Test
        }

        public class AnotherNestedClass {

            @Test
            public void testSomethingElse() {
                // Test
            }
        }
    }
}

Note that we don't need to specify Spring's runner here. Instead, we use the rules to apply the Spring test framework, available as of Spring 4.2 .

JUnit 5

Arguably a more future-proof solution would be to upgrade to JUnit 5 . Then you can construct test cases straight out of the box, with the @Nested annotation. See below:

@SpringBootTest
@ExtendWith(SpringExtension.class)
class MyNestedTest {

    @BeforeAll
    void setup() {
        // General test-suite setup
    }

    @Nested
    @DisplayName("parentTestSuite")
    class NestedClass {

        @Test
        void testSomething() {
            // Test
        }

        @Nested
        @DisplayName("childTestSuite")
        class AnotherNestedClass {

            @Test
            void testSomethingElse() {
                // Test
            }

        }
    }
}

Note that @RunWith has been replaced with @ExtendWith in JUnit 5 . If you choose to migrate to JUnit 5, you may find it useful to read Baeldung's guide on JUnit 5 migration .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM