Skip to content

Commit

Permalink
Verify that TestContextBootstrapper resolution ignores duplicates
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbrannen committed Jul 4, 2018
1 parent 18781d7 commit 755add3
Showing 1 changed file with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -27,7 +27,6 @@
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;
Expand All @@ -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);
Expand All @@ -57,16 +79,6 @@ public void resolveTestContextBootstrapperForWebAppConfigurationAnnotatedClass()
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);
Expand All @@ -83,18 +95,10 @@ public void resolveTestContextBootstrapperWithMetaBootstrapWithAnnotation() {
}

@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);
Expand All @@ -112,14 +116,24 @@ static class BarBootstrapper extends DefaultTestContextBootstrapper {}
@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 {}
Expand All @@ -129,10 +143,10 @@ static class InheritedBootstrapWithAnnotationClass extends DirectBootstrapWithAn
@BootWithBar
static class MetaAnnotatedBootstrapWithAnnotationClass {}

@BootWithBar
@BootWithFoo
static class DoubleMetaAnnotatedBootstrapWithAnnotationClass {}

@BootWithFooAgain
static class DuplicateMetaAnnotatedBootstrapWithAnnotationClass {}

@WebAppConfiguration
static class WebAppConfigurationAnnotatedClass {}

Expand Down

0 comments on commit 755add3

Please sign in to comment.