|
| 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 java.util.List; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 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.test.context.bean.override.convention.TestBean; |
| 27 | +import org.springframework.test.context.bean.override.example.ExampleService; |
| 28 | +import org.springframework.test.context.bean.override.example.RealExampleService; |
| 29 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.mockito.BDDMockito.given; |
| 33 | +import static org.springframework.test.mockito.MockitoAssertions.assertIsMock; |
| 34 | + |
| 35 | +/** |
| 36 | + * Integration tests for Bean Overrides where a {@link MockitoBean @MockitoBean} |
| 37 | + * overrides a {@link TestBean @TestBean} when trying to replace the same existing |
| 38 | + * bean, selected by-type. |
| 39 | + * |
| 40 | + * <p>In other words, this test class demonstrates how one Bean Override can |
| 41 | + * silently override another Bean Override. |
| 42 | + * |
| 43 | + * @author Sam Brannen |
| 44 | + * @since 6.2.1 |
| 45 | + * @see <a href="https://github.com/spring-projects/spring-framework/issues/34056">gh-34056</a> |
| 46 | + * @see MockitoBeanDuplicateTypeCreationIntegrationTests |
| 47 | + * @see MockitoBeanDuplicateTypeReplacementIntegrationTests |
| 48 | + */ |
| 49 | +@SpringJUnitConfig |
| 50 | +public class MockitoBeanOverridesTestBeanIntegrationTests { |
| 51 | + |
| 52 | + @TestBean |
| 53 | + ExampleService testService; |
| 54 | + |
| 55 | + @MockitoBean |
| 56 | + ExampleService mockService; |
| 57 | + |
| 58 | + @Autowired |
| 59 | + List<ExampleService> services; |
| 60 | + |
| 61 | + |
| 62 | + static ExampleService testService() { |
| 63 | + return new RealExampleService("@TestBean"); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + /** |
| 68 | + * One could argue that we would ideally expect an exception to be thrown when |
| 69 | + * two competing overrides are created to replace the same existing bean; however, |
| 70 | + * we currently only log a warning in such cases. |
| 71 | + * <p>This method therefore asserts the status quo in terms of behavior. |
| 72 | + * <p>And the log can be manually checked to verify that an appropriate |
| 73 | + * warning was logged. |
| 74 | + */ |
| 75 | + @Test |
| 76 | + void mockitoBeanShouldOverrideTestBean() { |
| 77 | + // Currently logs something similar to the following. |
| 78 | + // |
| 79 | + // WARN - Bean with name 'exampleService' was overridden by multiple handlers: |
| 80 | + // [TestBeanOverrideHandler@770beef5 ..., MockitoBeanOverrideHandler@6dd1f638 ...] |
| 81 | + |
| 82 | + // Last override wins... |
| 83 | + assertThat(services).containsExactly(mockService); |
| 84 | + assertThat(testService).isSameAs(mockService); |
| 85 | + |
| 86 | + assertIsMock(mockService); |
| 87 | + |
| 88 | + assertThat(mockService.greeting()).isNull(); |
| 89 | + given(mockService.greeting()).willReturn("mocked"); |
| 90 | + assertThat(mockService.greeting()).isEqualTo("mocked"); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + @Configuration |
| 95 | + static class Config { |
| 96 | + |
| 97 | + @Bean |
| 98 | + ExampleService exampleService() { |
| 99 | + return () -> "@Bean"; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | +} |
0 commit comments