Spring's @ContextConfiguration test configuration in a testng class is creating beans only once per test class, not per test

java, spring, testng, unit-testing

Solution

If you want the context to be reloaded on each test run, then you ought to be extending AbstractTestNGSpringContextTests and using the @DiritesContext annotation in addition to @ContextConfiguration. An example:

@ContextConfiguration(locations = {
        "classpath:yourconfig.xml"
})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class YourTestClass extends AbstractTestNGSpringContextTests {
 //magic
}

A classmode of Context.ClassMode.AFTER_EACH_TEST_METHOD will cause spring to reload the context after each test method is called. You can also use DirtiesContext.ClassMode.AFTER_CLASS to reload the context before each test class (useful to be used on an superclass for all spring enabled test classes).

Problem

Given a testng class that uses spring's test utility annotation ContextConfiguration to create beans, the beans are only created once for the life of the test class. Before using this, I always used the @BeforeMethod to rebuild everything before each @Test method. My question: Is there a way to have spring rebuild the beans for each @Test method? ``` //The beans are unfortunately created only once for the life of the class. @ContextConfiguration( locations = { "/path/to/my/test/beans.xml"}) public class Foo { @BeforeMethod public void setUp() throws Exception { //I am run for every test in the class } @AfterMethod public void tearDown() throws Exception { //I am run for every test in the class } @Test public void testNiceTest1() throws Exception { } @Test public void testNiceTest2() throws Exception { } } ```

Original source