Reputation: 1593
As mentioned in http://docs.spring.io/spring/docs/current/spring-framework-reference/html/testing.html#testcontext-ctx-management-caching, Spring will always tries to cache test contexts according to some generated key.
But, is there a way to cache a test context that failed to load? In other words -- if a test context failed to load, I do not want further tests to re-attempt loading it. In fact, they should just fail immediately with the same error that caused the initial context loading attempt to fail.
So, is there a way to do this in Spring? E.g., if I try to load a context whose "generated key" is the same as a previous one that failed to load, just fail immediately with the same error that the initial context loading attempt failed with/.
Upvotes: 1
Views: 830
Reputation: 21
One solution to this would be to create you're own ContextLoader and override the loadContext method. For example, for a test using WebAppConfiguration, you could override WebDelegatingSmartContextLoader with something along the lines of
public class FastFailContextLoader extends WebDelegatingSmartContextLoader {
private static boolean initialized = false;
@Override
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
if (initialized) {
throw new IllegalArgumentException(
"The ApplicationContext has already attempted to initialize. Aborting subsequent initialization. Check "
+ "earlier logs for original error");
}
setInitialized();
return super.loadContext(mergedConfig);
}
private static void setInitialized() {
initialized = true;
}
}
Then you just need to annotate your test with
@ContextConfiguration(loader = FastFailContextLoader.class)
Upvotes: 0
Reputation: 31177
No, as of Spring Framework 4.0.5 there is no mechanism for caching a failed ApplicationContext
.
If this is a feature you would like to see introduced in the Spring TestContext Framework, please create a JIRA issue for the "Spring Framework" project and "Test" component.
Regards,
Sam (author of the Spring TestContext Framework)
Upvotes: 1