X Tutup
Skip to content

Commit c54cdd6

Browse files
committed
Fail fast if @WebAppConfiguration and @SpringBootTest are used together
Closes spring-projectsgh-6795
1 parent 878a052 commit c54cdd6

File tree

4 files changed

+128
-47
lines changed

4 files changed

+128
-47
lines changed

spring-boot-test/src/main/java/org/springframework/boot/test/context/SpringBootTestContextBootstrapper.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.test.context;
1818

19+
import java.lang.annotation.Annotation;
1920
import java.util.ArrayList;
2021
import java.util.Arrays;
2122
import java.util.List;
@@ -72,6 +73,7 @@ public class SpringBootTestContextBootstrapper extends DefaultTestContextBootstr
7273
@Override
7374
public TestContext buildTestContext() {
7475
TestContext context = super.buildTestContext();
76+
verifyConfiguration(context.getTestClass());
7577
WebEnvironment webEnvironment = getWebEnvironment(context.getTestClass());
7678
if (webEnvironment == WebEnvironment.MOCK && hasWebEnvironmentClasses()) {
7779
context.setAttribute(ACTIVATE_SERVLET_LISTENER, true);
@@ -246,6 +248,19 @@ protected SpringBootTest getAnnotation(Class<?> testClass) {
246248
return AnnotatedElementUtils.getMergedAnnotation(testClass, SpringBootTest.class);
247249
}
248250

251+
protected void verifyConfiguration(Class<?> testClass) {
252+
if (getAnnotation(testClass) != null
253+
&& getAnnotation(WebAppConfiguration.class, testClass) != null) {
254+
throw new IllegalStateException("@WebAppConfiguration is unnecessary when "
255+
+ "using @SpringBootTest and should be removed");
256+
}
257+
}
258+
259+
private <T extends Annotation> T getAnnotation(Class<T> annotationType,
260+
Class<?> testClass) {
261+
return AnnotatedElementUtils.getMergedAnnotation(testClass, annotationType);
262+
}
263+
249264
/**
250265
* Create a new {@link MergedContextConfiguration} with different classes.
251266
* @param mergedConfig the source config

spring-boot-test/src/test/java/org/springframework/boot/test/context/bootstrap/SpringBootTestContextBootstrapperExampleConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import org.springframework.boot.SpringBootConfiguration;
2020

2121
/**
22-
* Example configuration used in {@link SpringBootTestContextBootstrapperTests}.
22+
* Example configuration used in {@link SpringBootTestContextBootstrapperIntegrationTests}.
2323
*
2424
* @author Phillip Webb
2525
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.context.bootstrap;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
24+
import org.springframework.boot.test.context.TestConfiguration;
25+
import org.springframework.context.ApplicationContext;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.stereotype.Component;
28+
import org.springframework.test.context.BootstrapWith;
29+
import org.springframework.test.context.junit4.SpringRunner;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Integration tests for {@link SpringBootTestContextBootstrapper} (in its own package so
35+
* we can test detection).
36+
*
37+
* @author Phillip Webb
38+
*/
39+
@RunWith(SpringRunner.class)
40+
@BootstrapWith(SpringBootTestContextBootstrapper.class)
41+
public class SpringBootTestContextBootstrapperIntegrationTests {
42+
43+
@Autowired
44+
private ApplicationContext context;
45+
46+
@Autowired
47+
private SpringBootTestContextBootstrapperExampleConfig config;
48+
49+
@Test
50+
public void findConfigAutomatically() throws Exception {
51+
assertThat(this.config).isNotNull();
52+
}
53+
54+
@Test
55+
public void contextWasCreatedViaSpringApplication() throws Exception {
56+
assertThat(this.context.getId()).startsWith("application:");
57+
}
58+
59+
@Test
60+
public void testConfigurationWasApplied() throws Exception {
61+
assertThat(this.context.getBean(ExampleBean.class)).isNotNull();
62+
}
63+
64+
@TestConfiguration
65+
static class TestConfig {
66+
67+
@Bean
68+
public ExampleBean exampleBean() {
69+
return new ExampleBean();
70+
}
71+
72+
}
73+
74+
static class ExampleBean {
75+
76+
}
77+
78+
@Component
79+
static class ExampleTestComponent {
80+
81+
}
82+
}

spring-boot-test/src/test/java/org/springframework/boot/test/context/bootstrap/SpringBootTestContextBootstrapperTests.java

Lines changed: 30 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,67 +16,51 @@
1616

1717
package org.springframework.boot.test.context.bootstrap;
1818

19+
import org.junit.Rule;
1920
import org.junit.Test;
20-
import org.junit.runner.RunWith;
21+
import org.junit.rules.ExpectedException;
2122

22-
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.context.SpringBootTest;
2324
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
24-
import org.springframework.boot.test.context.TestConfiguration;
25-
import org.springframework.context.ApplicationContext;
26-
import org.springframework.context.annotation.Bean;
27-
import org.springframework.stereotype.Component;
28-
import org.springframework.test.context.BootstrapWith;
29-
import org.springframework.test.context.junit4.SpringRunner;
25+
import org.springframework.test.context.BootstrapContext;
26+
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
27+
import org.springframework.test.context.web.WebAppConfiguration;
3028

31-
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.mockito.BDDMockito.given;
30+
import static org.mockito.Mockito.mock;
3231

3332
/**
34-
* Tests for {@link SpringBootTestContextBootstrapper} (in its own package so we can test
35-
* detection).
33+
* Tests for {@link SpringBootTestContextBootstrapper}.
3634
*
37-
* @author Phillip Webb
35+
* @author Andy Wilkinson
3836
*/
39-
@RunWith(SpringRunner.class)
40-
@BootstrapWith(SpringBootTestContextBootstrapper.class)
4137
public class SpringBootTestContextBootstrapperTests {
4238

43-
@Autowired
44-
private ApplicationContext context;
45-
46-
@Autowired
47-
private SpringBootTestContextBootstrapperExampleConfig config;
39+
@Rule
40+
public ExpectedException thrown = ExpectedException.none();
4841

42+
@SuppressWarnings("rawtypes")
4943
@Test
50-
public void findConfigAutomatically() throws Exception {
51-
assertThat(this.config).isNotNull();
44+
public void failFastWhenSpringBootTestAndWebAppConfigurationAreUsedTogether() {
45+
SpringBootTestContextBootstrapper bootstrapper = new SpringBootTestContextBootstrapper();
46+
BootstrapContext bootstrapContext = mock(BootstrapContext.class);
47+
bootstrapper.setBootstrapContext(bootstrapContext);
48+
given((Class) bootstrapContext.getTestClass())
49+
.willReturn(SpringBootTestAndWebAppConfiguration.class);
50+
CacheAwareContextLoaderDelegate contextLoaderDeleagte = mock(
51+
CacheAwareContextLoaderDelegate.class);
52+
given(bootstrapContext.getCacheAwareContextLoaderDelegate())
53+
.willReturn(contextLoaderDeleagte);
54+
this.thrown.expect(IllegalStateException.class);
55+
this.thrown.expectMessage("@WebAppConfiguration is unnecessary when using "
56+
+ "@SpringBootTest and should be removed");
57+
bootstrapper.buildTestContext();
5258
}
5359

54-
@Test
55-
public void contextWasCreatedViaSpringApplication() throws Exception {
56-
assertThat(this.context.getId()).startsWith("application:");
57-
}
60+
@SpringBootTest
61+
@WebAppConfiguration
62+
private static class SpringBootTestAndWebAppConfiguration {
5863

59-
@Test
60-
public void testConfigurationWasApplied() throws Exception {
61-
assertThat(this.context.getBean(ExampleBean.class)).isNotNull();
6264
}
6365

64-
@TestConfiguration
65-
static class TestConfig {
66-
67-
@Bean
68-
public ExampleBean exampleBean() {
69-
return new ExampleBean();
70-
}
71-
72-
}
73-
74-
static class ExampleBean {
75-
76-
}
77-
78-
@Component
79-
static class ExampleTestComponent {
80-
81-
}
8266
}

0 commit comments

Comments
 (0)
X Tutup