Each JUnit test method is assumed to be isolated, that is does not have any side effects that could cause another test method to behave differently. This can be achieved by modifying the state of beans that are managed by spring.
For example, say you have a bean managed by spring of class MySpringBean
which has a string property with a value of "string"
. The following test method testBeanString
will have a different result depending if it is called before or after the method testModify
.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/base-context.xml"})
public class SpringTests {
@Autowired
private MySpringBean bean;
@Test public void testModify() {
// dirties the state of a managed bean
bean.setString("newSring");
}
@Test pubic void testBeanString() {
assertEquals("string", bean.getString());
}
}
use the @DirtiesContext
annotation to indicate that the test method may change the state of spring managed beans.