How to force a fresh version of the Spring context BEFORE the test executes

前端 未结 3 1350
有刺的猬
有刺的猬 2021-02-18 15:54

I use the SpringJUnit4ClassRunner for writing integration tests. I also use @DirtiesContext for tests that leave the context in an broken state behind

3条回答
  •  心在旅途
    2021-02-18 16:36

    As of Spring 4.2 the DirtiesContext annotation supports the following new phases: BEFORE_CLASS, BEFORE_EACH_TEST_METHOD and BEFORE_METHOD. So you can now do for example:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(...)
    @DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)
    public class MyTest {
       ..
    }
    

提交回复
热议问题