|
| 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.integration; |
| 18 | + |
| 19 | +import java.lang.annotation.Retention; |
| 20 | +import java.lang.annotation.RetentionPolicy; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | + |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 27 | +import org.springframework.context.ApplicationContext; |
| 28 | +import org.springframework.context.annotation.Bean; |
| 29 | +import org.springframework.context.annotation.Configuration; |
| 30 | +import org.springframework.test.context.bean.override.example.ExampleService; |
| 31 | +import org.springframework.test.context.bean.override.example.ExampleServiceCaller; |
| 32 | +import org.springframework.test.context.bean.override.mockito.MockitoBean; |
| 33 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 34 | + |
| 35 | +import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.mockito.Mockito.when; |
| 37 | +import static org.springframework.test.mockito.MockitoAssertions.assertIsMock; |
| 38 | +import static org.springframework.test.mockito.MockitoAssertions.assertMockName; |
| 39 | + |
| 40 | +/** |
| 41 | + * Tests for {@link MockitoBean @MockitoBean} where the mocked bean is associated |
| 42 | + * with a custom {@link Qualifier @Qualifier} annotation and the bean to override |
| 43 | + * is selected by name. |
| 44 | + * |
| 45 | + * @author Sam Brannen |
| 46 | + * @since 6.2.6 |
| 47 | + * @see <a href="https://github.com/spring-projects/spring-framework/issues/34646">gh-34646</a> |
| 48 | + * @see MockitoBeanWithCustomQualifierAnnotationByTypeTests |
| 49 | + */ |
| 50 | +@ExtendWith(SpringExtension.class) |
| 51 | +class MockitoBeanWithCustomQualifierAnnotationByNameTests { |
| 52 | + |
| 53 | + @MockitoBean(name = "qualifiedService", enforceOverride = true) |
| 54 | + @MyQualifier |
| 55 | + ExampleService service; |
| 56 | + |
| 57 | + @Autowired |
| 58 | + ExampleServiceCaller caller; |
| 59 | + |
| 60 | + |
| 61 | + @Test |
| 62 | + void test(ApplicationContext context) { |
| 63 | + assertIsMock(service); |
| 64 | + assertMockName(service, "qualifiedService"); |
| 65 | + assertThat(service).isNotInstanceOf(QualifiedService.class); |
| 66 | + |
| 67 | + // Since the 'service' field's type is ExampleService, the QualifiedService |
| 68 | + // bean in the @Configuration class effectively gets removed from the context, |
| 69 | + // or rather it never gets created because we register an ExampleService as |
| 70 | + // a manual singleton in its place. |
| 71 | + assertThat(context.getBeanNamesForType(QualifiedService.class)).isEmpty(); |
| 72 | + assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(1); |
| 73 | + assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(1); |
| 74 | + |
| 75 | + when(service.greeting()).thenReturn("mock!"); |
| 76 | + assertThat(caller.sayGreeting()).isEqualTo("I say mock!"); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + @Configuration(proxyBeanMethods = false) |
| 81 | + static class Config { |
| 82 | + |
| 83 | + @Bean |
| 84 | + QualifiedService qualifiedService() { |
| 85 | + return new QualifiedService(); |
| 86 | + } |
| 87 | + |
| 88 | + @Bean |
| 89 | + ExampleServiceCaller myServiceCaller(@MyQualifier ExampleService service) { |
| 90 | + return new ExampleServiceCaller(service); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Qualifier |
| 95 | + @Retention(RetentionPolicy.RUNTIME) |
| 96 | + @interface MyQualifier { |
| 97 | + } |
| 98 | + |
| 99 | + @MyQualifier |
| 100 | + static class QualifiedService implements ExampleService { |
| 101 | + |
| 102 | + @Override |
| 103 | + public String greeting() { |
| 104 | + return "Qualified service"; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments