title: Spring & JUnit | How to disable spring test context caching for a specific test class?
body: <p>My code has many integration tests classes - <code>ServiceTest1</code>, <code>ServiceTest2</code> ... , <code>ServiceTestN</code>.
  All tests are executed with spring using the same test context (<code>MyAppContext.xml</code>).</p>
  <p>As shown in the snippet below, <code>ServiceTest1</code> test context requires one of its beans to be overridden, and that change is relevant <b>ONLY</b> for <code>ServiceTest1</code>.</p>
  <p>When executed, <code>ServiceTest1</code> works as expected. However, when executing the other tests (e.g. <code>ServiceTest2</code> in the snippet) it fails with spring initialization errors, since spring is caching <code>MyAppContext.xml</code> test context, and  <code>ServiceTest1</code> test context manipulation is conflicting with the other tests.</p>
  <p>I'm looking for a way to avoid caching of <code>ServiceTest1</code> test context (such as making its caching key unique, based on @ContextConfiguration).
  Note that fully disabling caching is not a wanted solution.</p>
  <p>Any ideas?</p>
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration
  public class ServiceTest1 {
  
      // some tests ...
  
      @Configuration
      @ImportResource({&quot;classpath*:MyAppContext.xml&quot;})
      static class Context {
  
          @Bean
          @Primary
          ServiceOne serviceOne() {
              // instantiate something ...
          }
      }
  }
  
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { &quot;classpath*:MyAppContext.xml&quot;})
  public class ServiceTest2 {
  
      // some tests ...
  
  }
  </code></pre>
  <p>Thanks!</p>
  <p><b>EDIT:</b><br>
  Spring initialization errors are:</p>
  <blockquote>
  <p>java.lang.IllegalStateException: Failed to load ApplicationContext</p>
  <p>...</p>
  <p>Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:/WEB-INF/applicationContext-security]</p>
  <p>Offending resource: URL [file:/path/to/MyAppContext.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Duplicate  detected.</p>
  <p>Offending resource: class path resource [WEB-INF/applicationContext-security.xml]</p>
  </blockquote>