title: How to enable a custom ContextCache in Spring Test
body: <p>In a parallel test environment I'd like to guarantee a new Context for each test (without forking JVMs). I'm bringing Spring into a Test framework that has some classes that maintain state - so a new one is required for each test execution.</p>
  
  <p>The <a href="http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html" rel="nofollow">docs</a> elude to creating a custom <code>ContextCache</code> but I cannot find any more details on how one would do this.</p>
  
  <p>I've been stepping through the Spring code while debugging a test and trying to figure out how I would get integrate my custom <code>ContextCache</code>. The method I have considered is almost certainly wrong, so seeking guidance on the way to do this.</p>
  
  <p>The test framework is <code>TestNG</code> based, so I have a base class like:</p>
  
  <pre><code>@ContextConfiguration(locations = {"classpath:one.xml", "classpath:two.xml"})
  public abstract BaseTest extends AbstractTestNGSpringContextTests
  </code></pre>
  
  <p>My current theory involves:</p>
  
  <ol>
  <li>Swapping out <code>AbstractTestNGSpringContextTests</code> for my own implementation. Pretty much a clone of <code>AbstractTestNGSpringContextTests</code> except to control the <code>TestContextManager</code> it instantiates in member declarations. (it can't be effectively subclassed)</li>
  <li>Subclassing <code>org.springframework.test.context.TestContextManager</code> (it has default scope dependencies) and overloading various methods to avoid the use of the <code>ContextCache</code> it creates in it's member declarations. This would delegate to my <code>NoOpContextCache</code> that would not cache any contexts. </li>
  </ol>
  
  <p>Using <code>spring-test 4.1.7</code>.</p>
  
  <p>Things I've tried but haven't worked:</p>
  
  <ol>
  <li><p>I've used <code>prototype</code> scope to no avail. The bean is declared as <code>@Scope("prototype")</code> and is <code>@Autowired</code> into <code>BaseTest</code> but I'm finding the same instance is being used in all tests that extend from <code>BaseTest</code>.</p></li>
  <li><p><code>@DirtiesContext</code> while it is doing it's job correctly, it's not useful when there are 20 tests using the same cached Context in parallel.</p></li>
  </ol>
  
  <p><strong>Update 3hrs later:</strong>
  My swapping out of <code>TestContextManager</code> etc did end up working with my <code>NoOpContextCache</code> but was very inelegant...ie ended up with an <code>org.springframework.test.context</code> package in my source to get around scope restrictions.</p>
  
  <p>I did however get the <code>prototype</code> way going by using:</p>
  
  <pre><code>@Configuration
  public class SpringConfiguration {
  
      @Bean
      @Scope("prototype")
      public MyStatefulBean get() {
          return new MyStatefulBean();
      }
  }
  </code></pre>
  
  <p>So that really gets me the outcome I was after, however I'll leave the question up as I would still like to know how to provide a custom <code>ContextCache</code>.</p>