Allow directly present @BootstrapWith to override meta annotations

Prior to this commit, if a test class was meta-annotated with multiple
@BootstrapWith declarations that registered different
TestContextBootstrapper implementations, such a configuration would
result in an IllegalStateException, and there was no way to override
this behavior.

This commit addresses this shortcoming by relaxing the explicit
TestContextBootstrapper resolution in BootstrapUtils so that a directly
present @BootstrapWith annotation will now override declarations of
@BootstrapWith that are meta-present. In other words, if @BootstrapWith
is used as a meta-annotation, it can be overridden directly on the test
class via an explicit, local declaration of @BootstrapWith.

Issue: SPR-17006
This commit is contained in:
Sam Brannen
2018-08-15 16:17:05 +02:00
parent 4d7b265258
commit d20d95b7ac
2 changed files with 27 additions and 4 deletions

View File

@@ -26,7 +26,6 @@ import org.springframework.beans.BeanUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/**
@@ -154,10 +153,19 @@ abstract class BootstrapUtils {
if (annotations.isEmpty()) {
return null;
}
Assert.state(annotations.size() <= 1, () -> String.format(
if (annotations.size() == 1) {
return annotations.iterator().next().value();
}
// Allow directly-present annotation to override annotations that are meta-present.
BootstrapWith bootstrapWith = testClass.getDeclaredAnnotation(BootstrapWith.class);
if (bootstrapWith != null) {
return bootstrapWith.value();
}
throw new IllegalStateException(String.format(
"Configuration error: found multiple declarations of @BootstrapWith for test class [%s]: %s",
testClass.getName(), annotations));
return annotations.iterator().next().value();
}
private static Class<?> resolveDefaultTestContextBootstrapper(Class<?> testClass) throws Exception {