mercredi 15 avril 2015

Spring test : strange behavior of context config caching between test classes ?

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...


I have 2 different tests :



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:myInterface-core.xml",
"classpath:myInterface-datasource-test.xml"})
public class PropertyConfigurerTest {


@Test
public void shouldResolvePropertyForOutPutFile(){

}
}


(it does nothing, simply loads the context, intentionnaly)


And another one, more complex with actual tests in it (skipping them in below snippet) :



@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:myInterface-core.xml",
"classpath:myInterface-rmi.xml",
"classpath:myInterface-datasource-test.xml"})
public class MontranMessagesFlowTest {
...
}


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.


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 2 messages don't go to same listener 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.


I have found 2 different workarounds :



  • adding @DirtiesContext(classMode=ClassMode.AFTER_CLASS) on my first test.

  • 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.


But still, I don't understand the rationale, and it looks like a bug to me.


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.


Does anybody have an explanation for this ? Bug or not ?


Thanks


Vincent


Aucun commentaire:

Enregistrer un commentaire