title: Spring test : strange behavior of context config caching between test classes ?
body: <p>I am writing tests for a Spring Integration project, and I am running into something strange : I've read about how Spring caches the context between tests and how we can force to clean the cache with @DirtiesContext annotation. However, I'm not able to explain the behavior I observe, and it makes me think it's maybe a bug...</p>
  
  <p>I have 2 different tests :</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = { "classpath:myInterface-core.xml",
                                      "classpath:myInterface-datasource-test.xml"})
  public class PropertyConfigurerTest {
  
  
     @Test
     public void shouldResolvePropertyForOutPutFile(){
  
     }
  }
  </code></pre>
  
  <p>(it does nothing, simply loads the context, intentionnaly)</p>
  
  <p>And another one, more complex with actual tests in it (skipping them in below snippet) :</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {
   "classpath:myInterface-core.xml",
   "classpath:myInterface-rmi.xml",
   "classpath:myInterface-datasource-test.xml"})
  public class MontranMessagesFlowTest {
  ...
  }
  </code></pre>
  
  <p>As you can see, these 2 tests don't load exactly the same config : second test loads one extra config file that is not required for first one. </p>
  
  <p>When I run these 2 tests one after the other, second test is failing : in a nutshell, the goal of the test is to insert 2 rows in inMemory DB, start my Spring Integration flow and assert with a listener (inside a jms:listener-container) that I've received 2 JMS messages on the outbound side. I see in debug mode that actually the <strong>2 messages don't go to same listener</strong> so I get one message instead of the 2 I expect. Somehow, the fact that I'm loading some elements of the context in first test (even if I don't do anything with them) has an impact on the second test.</p>
  
  <p>I have found 2 different workarounds :</p>
  
  <ul>
  <li>adding <code>@DirtiesContext(classMode=ClassMode.AFTER_CLASS)</code> on my first test.</li>
  <li>modify the list of Spring files that I load in my first test, so that it matches exactly the one defined in the second test. </li>
  </ul>
  
  <p>But still, I don't understand the rationale, and it looks like a bug to me.  </p>
  
  <p>I am using Spring Test 4.1.4.RELEASE. I've put the minimum code necessary in a separate project to be able to reproduce. I can share it if required. </p>
  
  <p>Does anybody have an explanation for this ? Bug or not ?</p>
  
  <p>Thanks</p>
  
  <p>Vincent</p>