'Make @SpringBootTest use a new application on every test

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 ?



Solution 1:[1]

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() {

    }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ken Chan