Verify @MockitoSpyBean can spy bean from FactoryBean with generics
This commit introduces a test which verifies that @MockitoSpyBean on a field with generics can be used to replace an existing bean with matching generics that's produced by a FactoryBean that's programmatically registered via an ImportBeanDefinitionRegistrar. However, the test is currently @Disabled until the fix for https://github.com/spring-projects/spring-boot/issues/40234 has been ported to Spring Framework. See gh-33742
This commit is contained in:
@@ -40,6 +40,7 @@ import static org.mockito.BDDMockito.then;
|
|||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 6.2
|
* @since 6.2
|
||||||
* @see MockitoBeanWithGenericsOnTestFieldForNewBeanIntegrationTests
|
* @see MockitoBeanWithGenericsOnTestFieldForNewBeanIntegrationTests
|
||||||
|
* @see MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests
|
||||||
*/
|
*/
|
||||||
@SpringJUnitConfig
|
@SpringJUnitConfig
|
||||||
class MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanIntegrationTests {
|
class MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanIntegrationTests {
|
||||||
@@ -60,7 +61,7 @@ class MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanIntegrationTest
|
|||||||
|
|
||||||
@Configuration(proxyBeanMethods = false)
|
@Configuration(proxyBeanMethods = false)
|
||||||
@Import({ ExampleGenericServiceCaller.class, IntegerExampleGenericService.class })
|
@Import({ ExampleGenericServiceCaller.class, IntegerExampleGenericService.class })
|
||||||
static class SpyBeanOnTestFieldForExistingBeanConfig {
|
static class Config {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
ExampleGenericService<String> simpleExampleStringGenericService() {
|
ExampleGenericService<String> simpleExampleStringGenericService() {
|
||||||
@@ -68,7 +69,6 @@ class MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanIntegrationTest
|
|||||||
// generic type instead of the actual implementation class.
|
// generic type instead of the actual implementation class.
|
||||||
return new StringExampleGenericService("Enigma");
|
return new StringExampleGenericService("Enigma");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,101 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2024 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.test.context.bean.override.mockito.integration;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Disabled;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.MockingDetails;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.FactoryBean;
|
||||||
|
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||||
|
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
|
||||||
|
import org.springframework.core.ResolvableType;
|
||||||
|
import org.springframework.core.type.AnnotationMetadata;
|
||||||
|
import org.springframework.test.context.bean.override.example.ExampleGenericService;
|
||||||
|
import org.springframework.test.context.bean.override.example.StringExampleGenericService;
|
||||||
|
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.mockingDetails;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that {@link MockitoSpyBean @MockitoSpyBean} on a field with generics can
|
||||||
|
* be used to replace an existing bean with matching generics that's produced by a
|
||||||
|
* {@link FactoryBean} that's programmatically registered via an
|
||||||
|
* {@link ImportBeanDefinitionRegistrar}.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
* @author Sam Brannen
|
||||||
|
* @since 6.2
|
||||||
|
* @see MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanIntegrationTests
|
||||||
|
*/
|
||||||
|
@SpringJUnitConfig
|
||||||
|
@Disabled("Disabled until https://github.com/spring-projects/spring-boot/issues/40234 is ported to Spring Framework")
|
||||||
|
class MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests {
|
||||||
|
|
||||||
|
@MockitoSpyBean("exampleService")
|
||||||
|
ExampleGenericService<String> exampleService;
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSpying() {
|
||||||
|
MockingDetails mockingDetails = mockingDetails(this.exampleService);
|
||||||
|
assertThat(mockingDetails.isSpy()).isTrue();
|
||||||
|
assertThat(mockingDetails.getMockCreationSettings().getSpiedInstance())
|
||||||
|
.isInstanceOf(StringExampleGenericService.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@Import(FactoryBeanRegistrar.class)
|
||||||
|
static class Config {
|
||||||
|
}
|
||||||
|
|
||||||
|
static class FactoryBeanRegistrar implements ImportBeanDefinitionRegistrar {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,
|
||||||
|
BeanDefinitionRegistry registry) {
|
||||||
|
|
||||||
|
RootBeanDefinition definition = new RootBeanDefinition(ExampleGenericServiceFactoryBean.class);
|
||||||
|
ResolvableType targetType = ResolvableType.forClassWithGenerics(
|
||||||
|
ExampleGenericServiceFactoryBean.class, null, ExampleGenericService.class);
|
||||||
|
definition.setTargetType(targetType);
|
||||||
|
registry.registerBeanDefinition("exampleService", definition);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class ExampleGenericServiceFactoryBean<T, U extends ExampleGenericService<T>> implements FactoryBean<U> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public U getObject() throws Exception {
|
||||||
|
return (U) new StringExampleGenericService("Enigma");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
|
public Class<ExampleGenericService> getObjectType() {
|
||||||
|
return ExampleGenericService.class;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user