We use spring-test (4.x) with junit. Is it possible to reset spring-test's context cache before a test suite runs (not after).
@DirtiesContext only seems to have options to reset the context after execution. We'd like a way to signal spring-test that a specific test suite should be run with a fresh non-cached context (so that we can easily override bean definitions in our tests). Whether this is an extension to @DirtiesContext or a new annotation such as @RunWithFreshContext doesn't matter as long as we can do so :-)
UPDATE:
As of Spring Framework 4.2, it will be possible to use
@DirtiesContextto close a testApplicationContextbefore a test class or test method. See JIRA issue SPR-12429 for details.No, as of Spring Framework 4.1.2, it is not currently possible to have the Spring TestContext Framework remove an
ApplicationContextfrom the cache before the execution of a test method or test class.However, it is possible to develop your own custom
TestExecutionListenerthat does this for you. For example, you could extendDirtiesContextTestExecutionListenerand execute itsdirtyContext(...)method in thebeforeTestClass(...)andbeforeTestMethod(...)methods as you see fit. Naturally, if you include your custom TestExecutionListener alongsideDirtiesContextTestExecutionListener, you wouldn't want your custom listener to perform the same actions ofafterTestMethod(...)andafterTestClass(...)fromDirtiesContextTestExecutionListener. So you might opt to override those two methods with no-ops or alternatively forgo declaration of theDirtiesContextTestExecutionListenerin favor of your custom extension.Regards,
Sam (component lead for
spring-test)