Feb 24th, 2011, 01:17 AM
[SOLVED] Test context always reloading for every Junit class
This plagued our project for a few months before I figured out what the problem was so I thought that I'd share this to save anyone else the pain and to also recommend to the Spring documentation maintainers to add something to the documentation for this gotcha.
We have a fairly size-able application code base (100,000+ lines of code). As the project has grown, our integration tests when executed by maven would take longer and longer and longer. It would take ~5-6 seconds to execute each Junit class that used @ContextConfiguration because it would reload the context for every class. Multiply this by a few dozen integration tests using @ContextConfig and you get the picture.
When executed in the IDE (STS) only the first one take this long then all subsequent classes using that same context would execute in approx 0.1 sec. or less.
It finally dawned on me that perhaps the way our surefire maven plugin was executing our unit tests was subverting Spring's context caching. Sure enough, I found out that our surefire plugin's forkMode was set to "pertest". Forking in this way prevents each forked test from using the cached context from another fork which forces Spring to have to reload the context for every Junit class using @ContextConfiguration.
The Spring documentation merely states that @ContextConfiguration is cached and doesn't go into much detail beyond that. I think it would be good to specify that the cache is in memory and thus will only be usable for tests run within the same process. Perhaps a Spring note (the ones with the little leaf icon and blockquote styling) would be good to call this out in the Testing chapter.
I hope this helps someone.
Last edited by Temujin_12; Feb 24th, 2011 at 01:21 AM.
Feb 27th, 2011, 12:02 PM
I've created SPR-7996 for the Spring team to look into this.