Browse Source

Verify that TestContextBootstrapper resolution ignores duplicates

This commit introduces a test to verify that multiple declarations of
@BootstrapWith that register the same TestContextBootstrapper type
(i.e., duplicates) do not result in an error.

Issue: SPR-17006
pull/1870/head
Sam Brannen 6 years ago
parent
commit
755add3087
  1. 70
      spring-test/src/test/java/org/springframework/test/context/BootstrapUtilsTests.java

70
spring-test/src/test/java/org/springframework/test/context/BootstrapUtilsTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
@ -27,7 +27,6 @@ import org.springframework.test.context.support.DefaultTestContextBootstrapper; @@ -27,7 +27,6 @@ import org.springframework.test.context.support.DefaultTestContextBootstrapper;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.context.web.WebTestContextBootstrapper;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
@ -47,6 +46,29 @@ public class BootstrapUtilsTests { @@ -47,6 +46,29 @@ public class BootstrapUtilsTests {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void resolveTestContextBootstrapperWithEmptyBootstrapWithAnnotation() {
BootstrapContext bootstrapContext = BootstrapTestUtils.buildBootstrapContext(EmptyBootstrapWithAnnotationClass.class, delegate);
exception.expect(IllegalStateException.class);
exception.expectMessage("Specify @BootstrapWith's 'value' attribute");
resolveTestContextBootstrapper(bootstrapContext);
}
@Test
public void resolveTestContextBootstrapperWithDoubleMetaBootstrapWithAnnotations() {
BootstrapContext bootstrapContext = BootstrapTestUtils.buildBootstrapContext(
DoubleMetaAnnotatedBootstrapWithAnnotationClass.class, delegate);
exception.expect(IllegalStateException.class);
exception.expectMessage("Configuration error: found multiple declarations of @BootstrapWith");
exception.expectMessage(FooBootstrapper.class.getName());
exception.expectMessage(BarBootstrapper.class.getName());
resolveTestContextBootstrapper(bootstrapContext);
}
@Test
public void resolveTestContextBootstrapperForNonAnnotatedClass() {
assertBootstrapper(NonAnnotatedClass.class, DefaultTestContextBootstrapper.class);
@ -57,16 +79,6 @@ public class BootstrapUtilsTests { @@ -57,16 +79,6 @@ public class BootstrapUtilsTests {
assertBootstrapper(WebAppConfigurationAnnotatedClass.class, WebTestContextBootstrapper.class);
}
@Test
public void resolveTestContextBootstrapperWithEmptyBootstrapWithAnnotation() {
BootstrapContext bootstrapContext = BootstrapTestUtils.buildBootstrapContext(EmptyBootstrapWithAnnotationClass.class, delegate);
exception.expect(IllegalStateException.class);
exception.expectMessage(containsString("Specify @BootstrapWith's 'value' attribute"));
resolveTestContextBootstrapper(bootstrapContext);
}
@Test
public void resolveTestContextBootstrapperWithDirectBootstrapWithAnnotation() {
assertBootstrapper(DirectBootstrapWithAnnotationClass.class, FooBootstrapper.class);
@ -83,18 +95,10 @@ public class BootstrapUtilsTests { @@ -83,18 +95,10 @@ public class BootstrapUtilsTests {
}
@Test
public void resolveTestContextBootstrapperWithDoubleMetaBootstrapWithAnnotation() {
BootstrapContext bootstrapContext = BootstrapTestUtils.buildBootstrapContext(
DoubleMetaAnnotatedBootstrapWithAnnotationClass.class, delegate);
exception.expect(IllegalStateException.class);
exception.expectMessage(containsString("found multiple declarations of @BootstrapWith"));
exception.expectMessage(containsString(FooBootstrapper.class.getName()));
exception.expectMessage(containsString(BarBootstrapper.class.getName()));
resolveTestContextBootstrapper(bootstrapContext);
public void resolveTestContextBootstrapperWithDuplicatingMetaBootstrapWithAnnotations() {
assertBootstrapper(DuplicateMetaAnnotatedBootstrapWithAnnotationClass.class, FooBootstrapper.class);
}
private void assertBootstrapper(Class<?> testClass, Class<?> expectedBootstrapper) {
BootstrapContext bootstrapContext = BootstrapTestUtils.buildBootstrapContext(testClass, delegate);
TestContextBootstrapper bootstrapper = resolveTestContextBootstrapper(bootstrapContext);
@ -112,14 +116,24 @@ public class BootstrapUtilsTests { @@ -112,14 +116,24 @@ public class BootstrapUtilsTests {
@Retention(RetentionPolicy.RUNTIME)
static @interface BootWithFoo {}
@BootstrapWith(FooBootstrapper.class)
@Retention(RetentionPolicy.RUNTIME)
static @interface BootWithFooAgain {}
@BootstrapWith(BarBootstrapper.class)
@Retention(RetentionPolicy.RUNTIME)
static @interface BootWithBar {}
static class NonAnnotatedClass {}
// Invalid
@BootstrapWith
static class EmptyBootstrapWithAnnotationClass {}
// Invalid
@BootWithBar
@BootWithFoo
static class DoubleMetaAnnotatedBootstrapWithAnnotationClass {}
static class NonAnnotatedClass {}
@BootstrapWith(FooBootstrapper.class)
static class DirectBootstrapWithAnnotationClass {}
@ -129,10 +143,10 @@ public class BootstrapUtilsTests { @@ -129,10 +143,10 @@ public class BootstrapUtilsTests {
@BootWithBar
static class MetaAnnotatedBootstrapWithAnnotationClass {}
@BootWithBar
@BootWithFoo
static class DoubleMetaAnnotatedBootstrapWithAnnotationClass {}
@BootWithFooAgain
static class DuplicateMetaAnnotatedBootstrapWithAnnotationClass {}
@WebAppConfiguration
static class WebAppConfigurationAnnotatedClass {}

Loading…
Cancel
Save