Yes, it is quite possible. All you have to do is use the same locations attributes in your test classes:
@ContextConfiguration(locations = "classpath:test-context.xml")
Spring caches application contexts with the locations attribute, so if locations appears a second time, Spring uses the same context rather than creating a new one.
I wrote an article about this feature: Accelerating Spring Integration Tests . It is also described in detail in the Spring documentation: 9.3.2.1 Context-sensitive management and caching .
This has an interesting implication. Since Spring does not know when JUnit is complete, it caches the entire context permanently and closes them using the JVM hookdown. This behavior (especially when you have many test classes with different locations ) can lead to excessive memory usage, memory leaks, etc. Another advantage of the cache context.
Tomasz Nurkiewicz Dec 14 '11 at 9:22 a.m. 2011-12-14 09:22
source share