|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 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 | + * https://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.test.context.bean.override.mockito; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.beans.factory.BeanCreationException; |
| 22 | +import org.springframework.beans.factory.FactoryBean; |
| 23 | +import org.springframework.beans.factory.annotation.Autowired; |
| 24 | +import org.springframework.context.annotation.Bean; |
| 25 | +import org.springframework.context.annotation.Configuration; |
| 26 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 27 | + |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; |
| 29 | +import static org.mockito.BDDMockito.when; |
| 30 | + |
| 31 | +/** |
| 32 | + * Test {@link MockitoBean @MockitoBean} for a {@link FactoryBean} that is |
| 33 | + * "broken" or not able to be eagerly initialized. |
| 34 | + * |
| 35 | + * @author Sam Brannen |
| 36 | + * @author Simon Baslé |
| 37 | + */ |
| 38 | +@SpringJUnitConfig |
| 39 | +public class MockitoBeanForBrokenFactoryBeanIntegrationTests { |
| 40 | + |
| 41 | + @MockitoBean |
| 42 | + TestBean testBean; |
| 43 | + |
| 44 | + |
| 45 | + @Test |
| 46 | + void beanReturnedByFactoryIsMocked(@Autowired TestBean autowiredTestBean) { |
| 47 | + assertThat(autowiredTestBean).isSameAs(testBean); |
| 48 | + |
| 49 | + when(testBean.hello()).thenReturn("mock"); |
| 50 | + |
| 51 | + assertThat(testBean.hello()).isEqualTo("mock"); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + @Configuration(proxyBeanMethods = false) |
| 56 | + static class Config { |
| 57 | + |
| 58 | + @Bean |
| 59 | + TestFactoryBean testFactoryBean() { |
| 60 | + return new TestFactoryBean(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + static class TestFactoryBean implements FactoryBean<TestBean> { |
| 65 | + |
| 66 | + TestFactoryBean() { |
| 67 | + throw new BeanCreationException("simulating missing dependencies"); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public TestBean getObject() { |
| 72 | + return () -> "prod"; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Class<?> getObjectType() { |
| 77 | + return TestBean.class; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + interface TestBean { |
| 82 | + |
| 83 | + String hello(); |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments