Skip to content

Commit 138b96c

Browse files
committed
Use unique testRestTemplate bean name
Update `SpringBootTestContextCustomizer` to use the full qualified TestRestTemplate as the registered bean name. Prior to this commit it was possible that the customizer would replace the relatively common bean name `testRestTemplate`. Fixes gh-7711
1 parent c23d8fb commit 138b96c

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private void registerTestRestTemplate(ConfigurableApplicationContext context) {
6262

6363
private void registerTestRestTemplate(ConfigurableApplicationContext context,
6464
BeanDefinitionRegistry registry) {
65-
registry.registerBeanDefinition("testRestTemplate",
65+
registry.registerBeanDefinition(TestRestTemplate.class.getName(),
6666
new RootBeanDefinition(TestRestTemplateFactory.class));
6767
}
6868

spring-boot-test/src/test/java/org/springframework/boot/test/context/AbstractSpringBootTestEmbeddedWebEnvironmentTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public abstract class AbstractSpringBootTestEmbeddedWebEnvironmentTests {
6060
@Autowired
6161
private TestRestTemplate restTemplate;
6262

63+
public WebApplicationContext getContext() {
64+
return this.context;
65+
}
66+
6367
public TestRestTemplate getRestTemplate() {
6468
return this.restTemplate;
6569
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.test.annotation.DirtiesContext;
26+
import org.springframework.test.context.junit4.SpringRunner;
27+
import org.springframework.web.bind.annotation.RestController;
28+
import org.springframework.web.client.RestTemplate;
29+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Tests for {@link SpringBootTest} configured with {@link WebEnvironment#RANDOM_PORT}.
35+
*
36+
* @author Phillip Webb
37+
* @author Andy Wilkinson
38+
*/
39+
@RunWith(SpringRunner.class)
40+
@DirtiesContext
41+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "value=123" })
42+
public class SpringBootTestTestRestTemplateDefinedByUser
43+
extends AbstractSpringBootTestEmbeddedWebEnvironmentTests {
44+
45+
@Test
46+
public void restTemplateIsUserDefined() throws Exception {
47+
assertThat(getContext().getBean("testRestTemplate"))
48+
.isInstanceOf(RestTemplate.class);
49+
}
50+
51+
// gh-7711
52+
53+
@Configuration
54+
@EnableWebMvc
55+
@RestController
56+
protected static class Config extends AbstractConfig {
57+
58+
@Bean
59+
public RestTemplate testRestTemplate() {
60+
return new RestTemplate();
61+
}
62+
63+
}
64+
65+
}

0 commit comments

Comments
 (0)