|
| 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.integration; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Disabled; |
| 20 | +import org.junit.jupiter.api.Test; |
| 21 | +import org.mockito.MockingDetails; |
| 22 | + |
| 23 | +import org.springframework.beans.factory.FactoryBean; |
| 24 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 25 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 26 | +import org.springframework.context.annotation.Configuration; |
| 27 | +import org.springframework.context.annotation.Import; |
| 28 | +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; |
| 29 | +import org.springframework.core.ResolvableType; |
| 30 | +import org.springframework.core.type.AnnotationMetadata; |
| 31 | +import org.springframework.test.context.bean.override.example.ExampleGenericService; |
| 32 | +import org.springframework.test.context.bean.override.example.StringExampleGenericService; |
| 33 | +import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
| 34 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | +import static org.mockito.Mockito.mockingDetails; |
| 38 | + |
| 39 | +/** |
| 40 | + * Tests that {@link MockitoSpyBean @MockitoSpyBean} on a field with generics can |
| 41 | + * be used to replace an existing bean with matching generics that's produced by a |
| 42 | + * {@link FactoryBean} that's programmatically registered via an |
| 43 | + * {@link ImportBeanDefinitionRegistrar}. |
| 44 | + * |
| 45 | + * @author Andy Wilkinson |
| 46 | + * @author Sam Brannen |
| 47 | + * @since 6.2 |
| 48 | + * @see MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanIntegrationTests |
| 49 | + */ |
| 50 | +@SpringJUnitConfig |
| 51 | +@Disabled("Disabled until https://github.com/spring-projects/spring-boot/issues/40234 is ported to Spring Framework") |
| 52 | +class MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests { |
| 53 | + |
| 54 | + @MockitoSpyBean("exampleService") |
| 55 | + ExampleGenericService<String> exampleService; |
| 56 | + |
| 57 | + |
| 58 | + @Test |
| 59 | + void testSpying() { |
| 60 | + MockingDetails mockingDetails = mockingDetails(this.exampleService); |
| 61 | + assertThat(mockingDetails.isSpy()).isTrue(); |
| 62 | + assertThat(mockingDetails.getMockCreationSettings().getSpiedInstance()) |
| 63 | + .isInstanceOf(StringExampleGenericService.class); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + @Configuration(proxyBeanMethods = false) |
| 68 | + @Import(FactoryBeanRegistrar.class) |
| 69 | + static class Config { |
| 70 | + } |
| 71 | + |
| 72 | + static class FactoryBeanRegistrar implements ImportBeanDefinitionRegistrar { |
| 73 | + |
| 74 | + @Override |
| 75 | + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, |
| 76 | + BeanDefinitionRegistry registry) { |
| 77 | + |
| 78 | + RootBeanDefinition definition = new RootBeanDefinition(ExampleGenericServiceFactoryBean.class); |
| 79 | + ResolvableType targetType = ResolvableType.forClassWithGenerics( |
| 80 | + ExampleGenericServiceFactoryBean.class, null, ExampleGenericService.class); |
| 81 | + definition.setTargetType(targetType); |
| 82 | + registry.registerBeanDefinition("exampleService", definition); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + static class ExampleGenericServiceFactoryBean<T, U extends ExampleGenericService<T>> implements FactoryBean<U> { |
| 87 | + |
| 88 | + @Override |
| 89 | + @SuppressWarnings("unchecked") |
| 90 | + public U getObject() throws Exception { |
| 91 | + return (U) new StringExampleGenericService("Enigma"); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + @SuppressWarnings("rawtypes") |
| 96 | + public Class<ExampleGenericService> getObjectType() { |
| 97 | + return ExampleGenericService.class; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments