Fail fast if @WebAppConfiguration and @SpringBootTest are used together

Closes gh-6795
This commit is contained in:
Andy Wilkinson
2016-09-14 10:43:53 +01:00
parent 878a052567
commit c54cdd6735
4 changed files with 128 additions and 47 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.boot.test.context;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -72,6 +73,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
@Override
public TestContext buildTestContext() {
TestContext context = super.buildTestContext();
verifyConfiguration(context.getTestClass());
WebEnvironment webEnvironment = getWebEnvironment(context.getTestClass());
if (webEnvironment == WebEnvironment.MOCK && hasWebEnvironmentClasses()) {
context.setAttribute(ACTIVATE_SERVLET_LISTENER, true);
@@ -246,6 +248,19 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
return AnnotatedElementUtils.getMergedAnnotation(testClass, SpringBootTest.class);
}
protected void verifyConfiguration(Class<?> testClass) {
if (getAnnotation(testClass) != null
&& getAnnotation(WebAppConfiguration.class, testClass) != null) {
throw new IllegalStateException("@WebAppConfiguration is unnecessary when "
+ "using @SpringBootTest and should be removed");
}
}
private <T extends Annotation> T getAnnotation(Class<T> annotationType,
Class<?> testClass) {
return AnnotatedElementUtils.getMergedAnnotation(testClass, annotationType);
}
/**
* Create a new {@link MergedContextConfiguration} with different classes.
* @param mergedConfig the source config