|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 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 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 21 | + |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.context.ApplicationContext; |
| 24 | +import org.springframework.test.context.bean.override.example.ExampleService; |
| 25 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | +import static org.springframework.test.mockito.MockitoAssertions.assertIsMock; |
| 29 | + |
| 30 | +/** |
| 31 | + * Integration tests for {@link MockitoBean @MockitoBean} which verify that type-level |
| 32 | + * {@code @MockitoBean} declarations are not discovered more than once when searching |
| 33 | + * a type hierarchy, when an interface is <em>present</em> twice in the hierarchy. |
| 34 | + * |
| 35 | + * @author Sam Brannen |
| 36 | + * @since 6.2.7 |
| 37 | + * @see MockitoBeanNestedAndTypeHierarchiesWithEnclosingClassPresentTwiceTests |
| 38 | + * @see MockitoBeanNestedAndTypeHierarchiesWithSuperclassPresentTwiceTests |
| 39 | + * @see <a href="https://github.com/spring-projects/spring-framework/issues/34844">gh-34844</a> |
| 40 | + */ |
| 41 | +class MockitoBeanWithInterfacePresentTwiceTests extends AbstractMockitoBeanWithInterfacePresentTwiceTests |
| 42 | + implements MockConfigInterface { |
| 43 | + |
| 44 | + @Test |
| 45 | + void test(ApplicationContext context) { |
| 46 | + assertIsMock(service); |
| 47 | + assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(1); |
| 48 | + |
| 49 | + // The following are prerequisites for the tested scenario. |
| 50 | + assertThat(getClass().getInterfaces()).containsExactly(MockConfigInterface.class); |
| 51 | + assertThat(getClass().getSuperclass().getInterfaces()).containsExactly(MockConfigInterface.class); |
| 52 | + } |
| 53 | + |
| 54 | +} |
| 55 | + |
| 56 | +@MockitoBean(types = ExampleService.class) |
| 57 | +interface MockConfigInterface { |
| 58 | +} |
| 59 | + |
| 60 | +@ExtendWith(SpringExtension.class) |
| 61 | +abstract class AbstractMockitoBeanWithInterfacePresentTwiceTests implements MockConfigInterface { |
| 62 | + |
| 63 | + @Autowired |
| 64 | + ExampleService service; |
| 65 | + |
| 66 | +} |
0 commit comments