Skip to content

Commit

Permalink
Verify @⁠MockitoSpyBean can spy bean from FactoryBean with generics
Browse files Browse the repository at this point in the history
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
sbrannen committed Oct 29, 2024
1 parent 578928d commit 40960fa
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* @author Sam Brannen
* @since 6.2
* @see MockitoBeanWithGenericsOnTestFieldForNewBeanIntegrationTests
* @see MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanProducedByFactoryBeanIntegrationTests
*/
@SpringJUnitConfig
class MockitoSpyBeanWithGenericsOnTestFieldForExistingGenericBeanIntegrationTests {
Expand All @@ -60,15 +61,14 @@ void testSpying() {

@Configuration(proxyBeanMethods = false)
@Import({ ExampleGenericServiceCaller.class, IntegerExampleGenericService.class })
static class SpyBeanOnTestFieldForExistingBeanConfig {
static class Config {

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

}

}
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;
}
}

}

0 comments on commit 40960fa

Please sign in to comment.