Yes, this is perfectly possible. All you have to do is to use the same locations
attribute in your test classes:
@ContextConfiguration(locations = "classpath:test-context.xml")
Spring caches application contexts by locations
attribute so if the same locations
appears for the second time, Spring uses the same context rather than creating a new one.
I wrote an article about this feature: Speeding up Spring integration tests. Also it is described in details in Spring documentation: 9.3.2.1 Context management and caching.
This has an interesting implication. Because Spring does not know when JUnit is done, it caches all context forever and closes them using JVM shutdown hook. This behavior (especially when you have a lot of test classes with different locations
) might lead to excessive memory usage, memory leaks, etc. Another advantage of caching context.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…