|
| 1 | +/* |
| 2 | + * Copyright 2012-2019 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.web.client; |
| 18 | + |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +import org.springframework.beans.BeansException; |
| 22 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 23 | +import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
| 24 | +import org.springframework.boot.test.context.SpringBootTest; |
| 25 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 26 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 27 | +import org.springframework.context.support.AbstractApplicationContext; |
| 28 | +import org.springframework.test.context.MergedContextConfiguration; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.mockito.BDDMockito.given; |
| 32 | +import static org.mockito.Mockito.mock; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests for {@link TestRestTemplateContextCustomizer}. |
| 36 | + * |
| 37 | + * @author Andy Wilkinson |
| 38 | + */ |
| 39 | +public class TestRestTemplateContextCustomizerTests { |
| 40 | + |
| 41 | + @Test |
| 42 | + @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 43 | + public void whenContextIsNotABeanDefinitionRegistryTestRestTemplateIsRegistered() { |
| 44 | + new ApplicationContextRunner(TestApplicationContext::new) |
| 45 | + .withInitializer((context) -> { |
| 46 | + MergedContextConfiguration configuration = mock( |
| 47 | + MergedContextConfiguration.class); |
| 48 | + given(configuration.getTestClass()) |
| 49 | + .willReturn((Class) TestClass.class); |
| 50 | + new TestRestTemplateContextCustomizer().customizeContext(context, |
| 51 | + configuration); |
| 52 | + }).run((context) -> assertThat(context) |
| 53 | + .hasSingleBean(TestRestTemplate.class)); |
| 54 | + } |
| 55 | + |
| 56 | + @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
| 57 | + static class TestClass { |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + private static class TestApplicationContext extends AbstractApplicationContext { |
| 62 | + |
| 63 | + private final ConfigurableListableBeanFactory beanFactory = new DefaultListableBeanFactory(); |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void refreshBeanFactory() throws BeansException, IllegalStateException { |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void closeBeanFactory() { |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public ConfigurableListableBeanFactory getBeanFactory() |
| 76 | + throws IllegalStateException { |
| 77 | + return this.beanFactory; |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments