|
| 1 | +/* |
| 2 | + * Copyright 2012-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.BeforeEach; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 22 | + |
| 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.context.annotation.Scope; |
| 27 | +import org.springframework.context.annotation.ScopedProxyMode; |
| 28 | +import org.springframework.test.context.bean.override.example.ExampleService; |
| 29 | +import org.springframework.test.context.bean.override.example.ExampleServiceCaller; |
| 30 | +import org.springframework.test.context.bean.override.example.FailingExampleService; |
| 31 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.mockito.BDDMockito.given; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for {@link MockitoBean @MockitoBean} used in combination with scoped-proxy |
| 38 | + * targets. |
| 39 | + * |
| 40 | + * @author Phillip Webb |
| 41 | + * @author Sam Brannen |
| 42 | + * @since 6.2 |
| 43 | + * @see <a href="https://github.com/spring-projects/spring-boot/issues/5724">gh-5724</a> |
| 44 | + */ |
| 45 | +@ExtendWith(SpringExtension.class) |
| 46 | +public class MockitoBeanAndScopedProxyTests { |
| 47 | + |
| 48 | + @MockitoBean |
| 49 | + // The ExampleService mock should replace the scoped-proxy FailingExampleService |
| 50 | + // created in the @Configuration class. |
| 51 | + ExampleService service; |
| 52 | + |
| 53 | + @Autowired |
| 54 | + ExampleServiceCaller serviceCaller; |
| 55 | + |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + void configureServiceMock() { |
| 59 | + given(service.greeting()).willReturn("mock"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testMocking() { |
| 64 | + assertThat(serviceCaller.sayGreeting()).isEqualTo("I say mock"); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + @Configuration(proxyBeanMethods = false) |
| 69 | + static class Config { |
| 70 | + |
| 71 | + @Bean |
| 72 | + @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) |
| 73 | + ExampleService exampleService() { |
| 74 | + return new FailingExampleService(); |
| 75 | + } |
| 76 | + |
| 77 | + @Bean |
| 78 | + ExampleServiceCaller serviceCaller(ExampleService service) { |
| 79 | + return new ExampleServiceCaller(service); |
| 80 | + } |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | +} |
0 commit comments