-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
...anWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
||
} |