Make @SpringBootTest use a new application on every test

Viewed 204

Is there any way to make Spring boot use a completely fresh ApplicationContext on every single @Test method execution and discard the previous application context ?

Anyway to change the default behavior of reusing ApplicationContext ?

1 Answers

You can annotate a test method with @DirtiesContext to indicate the ApplicationContext after running this test method is dirty such that when it executes the next test method , it will completely refresh the ApplicationContext :

@SpringBootTest
public class FooTest {

    @Test
    @DirtiesContext
    public void test1() {

    }

    @Test
    @DirtiesContext
    public void test2() {

    }

}
Related