|
|
@ -16,11 +16,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.boot.test.context;
|
|
|
|
package org.springframework.boot.test.context;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.BeansException;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.FactoryBean;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
|
|
|
import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler;
|
|
|
|
import org.springframework.boot.test.web.client.LocalHostUriTemplateHandler;
|
|
|
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
|
|
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
|
|
|
|
|
|
|
import org.springframework.boot.web.client.RestTemplateBuilder;
|
|
|
|
|
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
|
|
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
import org.springframework.context.ConfigurableApplicationContext;
|
|
|
|
import org.springframework.core.annotation.AnnotatedElementUtils;
|
|
|
|
import org.springframework.core.annotation.AnnotatedElementUtils;
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
|
|
|
import org.springframework.test.context.ContextCustomizer;
|
|
|
|
import org.springframework.test.context.ContextCustomizer;
|
|
|
|
import org.springframework.test.context.MergedContextConfiguration;
|
|
|
|
import org.springframework.test.context.MergedContextConfiguration;
|
|
|
|
|
|
|
|
|
|
|
@ -38,12 +46,24 @@ class SpringBootTestContextCustomizer implements ContextCustomizer {
|
|
|
|
SpringBootTest annotation = AnnotatedElementUtils.getMergedAnnotation(
|
|
|
|
SpringBootTest annotation = AnnotatedElementUtils.getMergedAnnotation(
|
|
|
|
mergedContextConfiguration.getTestClass(), SpringBootTest.class);
|
|
|
|
mergedContextConfiguration.getTestClass(), SpringBootTest.class);
|
|
|
|
if (annotation.webEnvironment().isEmbedded()) {
|
|
|
|
if (annotation.webEnvironment().isEmbedded()) {
|
|
|
|
Object restTemplate = TestRestTemplateFactory
|
|
|
|
registerTestRestTemplate(context);
|
|
|
|
.createRestTemplate(context.getEnvironment());
|
|
|
|
|
|
|
|
context.getBeanFactory().registerSingleton("testRestTemplate", restTemplate);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void registerTestRestTemplate(ConfigurableApplicationContext context) {
|
|
|
|
|
|
|
|
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
|
|
|
|
|
|
|
|
if (beanFactory instanceof BeanDefinitionRegistry) {
|
|
|
|
|
|
|
|
registerTestRestTemplate(context, (BeanDefinitionRegistry) context);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void registerTestRestTemplate(ConfigurableApplicationContext context,
|
|
|
|
|
|
|
|
BeanDefinitionRegistry registry) {
|
|
|
|
|
|
|
|
registry.registerBeanDefinition("testRestTemplate",
|
|
|
|
|
|
|
|
new RootBeanDefinition(TestRestTemplateFactory.class));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
public int hashCode() {
|
|
|
|
return getClass().hashCode();
|
|
|
|
return getClass().hashCode();
|
|
|
@ -57,13 +77,47 @@ class SpringBootTestContextCustomizer implements ContextCustomizer {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Inner class to avoid references to web classes that may not be on the classpath
|
|
|
|
/**
|
|
|
|
private static class TestRestTemplateFactory {
|
|
|
|
* {@link FactoryBean} used to create a configure a {@link TestRestTemplate}.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public static class TestRestTemplateFactory
|
|
|
|
|
|
|
|
implements FactoryBean<TestRestTemplate>, ApplicationContextAware {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private TestRestTemplate object;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void setApplicationContext(ApplicationContext applicationContext)
|
|
|
|
|
|
|
|
throws BeansException {
|
|
|
|
|
|
|
|
RestTemplateBuilder builder = getRestTemplateBuilder(applicationContext);
|
|
|
|
|
|
|
|
TestRestTemplate template = new TestRestTemplate(builder.build());
|
|
|
|
|
|
|
|
template.setUriTemplateHandler(
|
|
|
|
|
|
|
|
new LocalHostUriTemplateHandler(applicationContext.getEnvironment()));
|
|
|
|
|
|
|
|
this.object = template;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private RestTemplateBuilder getRestTemplateBuilder(
|
|
|
|
|
|
|
|
ApplicationContext applicationContext) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
return applicationContext.getBean(RestTemplateBuilder.class);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (NoSuchBeanDefinitionException ex) {
|
|
|
|
|
|
|
|
return new RestTemplateBuilder();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public boolean isSingleton() {
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Class<?> getObjectType() {
|
|
|
|
|
|
|
|
return TestRestTemplate.class;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static TestRestTemplate createRestTemplate(Environment environment) {
|
|
|
|
@Override
|
|
|
|
TestRestTemplate template = new TestRestTemplate();
|
|
|
|
public TestRestTemplate getObject() throws Exception {
|
|
|
|
template.setUriTemplateHandler(new LocalHostUriTemplateHandler(environment));
|
|
|
|
return this.object;
|
|
|
|
return template;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|