Improve ex msg when locations & classes are declared in test hierarchy

Prior to this commit, if both locations and classes were declared via
@ContextConfiguration at differing levels in a test class hierarchy,
the exception message stated that neither of the default context
loaders was able to load an ApplicationContext from the merged context
configuration, but the message didn't explain why.

This commit adds an explicit check for such scenarios and provides a
more informative exception message similar to the following:

"Neither X nor Y supports loading an ApplicationContext from
[MergedContextConfiguration ...]: declare either 'locations' or
'classes' but not both."

Issue: SPR-12060
This commit is contained in:
Sam Brannen
2014-08-14 01:23:11 +02:00
parent f4c23d8715
commit 181299cc6c
2 changed files with 57 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,12 +16,9 @@
package org.springframework.test.context.support;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -31,6 +28,9 @@ import org.springframework.test.context.ContextLoader;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.util.ObjectUtils;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
* Unit tests for {@link DelegatingSmartContextLoader}.
*
@@ -44,6 +44,9 @@ public class DelegatingSmartContextLoaderTests {
private final DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader();
@Rule
public ExpectedException expectedException = ExpectedException.none();
private static void assertEmpty(Object[] array) {
assertTrue(ObjectUtils.isEmpty(array));
@@ -51,8 +54,12 @@ public class DelegatingSmartContextLoaderTests {
// --- SmartContextLoader - processContextConfiguration() ------------------
@Test(expected = IllegalStateException.class)
@Test
public void processContextConfigurationWithoutLocationsAndConfigurationClassesForBogusTestClass() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(startsWith("Neither"));
expectedException.expectMessage(containsString("was able to detect defaults"));
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes(getClass(),
EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, true, null, true, ContextLoader.class);
loader.processContextConfiguration(configAttributes);
@@ -76,8 +83,11 @@ public class DelegatingSmartContextLoaderTests {
assertEmpty(configAttributes.getLocations());
}
@Test(expected = IllegalStateException.class)
@Test
public void processContextConfigurationWithDefaultXmlConfigAndConfigurationClassGeneration() {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(containsString("both default locations AND default configuration classes were detected"));
ContextConfigurationAttributes configAttributes = new ContextConfigurationAttributes(
ImproperDuplicateDefaultXmlAndConfigClassTestCase.class, EMPTY_STRING_ARRAY, EMPTY_CLASS_ARRAY, true, null,
true, ContextLoader.class);
@@ -112,13 +122,31 @@ public class DelegatingSmartContextLoaderTests {
loader.loadContext(mergedConfig);
}
@Test(expected = IllegalStateException.class)
@Test
public void loadContextWithoutLocationsAndConfigurationClasses() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(startsWith("Neither"));
expectedException.expectMessage(containsString("was able to load an ApplicationContext from"));
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
loader.loadContext(mergedConfig);
}
/**
* @since 4.1
*/
@Test
public void loadContextWithLocationsAndConfigurationClasses() throws Exception {
expectedException.expect(IllegalStateException.class);
expectedException.expectMessage(startsWith("Neither"));
expectedException.expectMessage(endsWith("declare either 'locations' or 'classes' but not both."));
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(),
new String[] { "test.xml" }, new Class[] { getClass() }, EMPTY_STRING_ARRAY, loader);
loader.loadContext(mergedConfig);
}
private void assertApplicationContextLoadsAndContainsFooString(MergedContextConfiguration mergedConfig)
throws Exception {
ApplicationContext applicationContext = loader.loadContext(mergedConfig);