Skip to content

Commit 40960fa

Browse files
committed
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 spring-projects/spring-boot#40234 has been ported to Spring Framework. See gh-33742
1 parent 578928d commit 40960fa

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

Diff for: spring-test/src/test/java/org/springframework/test/context/bean/override/mockito/integration/MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanIntegrationTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Sam Brannen
4141
* @since 6.2
4242
* @see MockitoBeanWithGenericsOnTestFieldForNewBeanIntegrationTests
43+
* @see MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests
4344
*/
4445
@SpringJUnitConfig
4546
class MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanIntegrationTests {
@@ -60,15 +61,14 @@ void testSpying() {
6061

6162
@Configuration(proxyBeanMethods = false)
6263
@Import({ ExampleGenericServiceCaller.class, IntegerExampleGenericService.class })
63-
static class SpyBeanOnTestFieldForExistingBeanConfig {
64+
static class Config {
6465

6566
@Bean
6667
ExampleGenericService<String> simpleExampleStringGenericService() {
6768
// In order to trigger the issue, we need a method signature that returns the
6869
// generic type instead of the actual implementation class.
6970
return new StringExampleGenericService("Enigma");
7071
}
71-
7272
}
7373

7474
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)