Merge branch '6.2.x'
This commit is contained in:
@@ -153,6 +153,7 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
|
|||||||
// an existing bean definition.
|
// an existing bean definition.
|
||||||
if (beanFactory.containsBeanDefinition(beanName)) {
|
if (beanFactory.containsBeanDefinition(beanName)) {
|
||||||
existingBeanDefinition = beanFactory.getBeanDefinition(beanName);
|
existingBeanDefinition = beanFactory.getBeanDefinition(beanName);
|
||||||
|
setQualifiedElement(existingBeanDefinition, handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -167,6 +168,7 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
|
|||||||
if (candidates.contains(beanName)) {
|
if (candidates.contains(beanName)) {
|
||||||
// 3) We are overriding an existing bean by-name.
|
// 3) We are overriding an existing bean by-name.
|
||||||
existingBeanDefinition = beanFactory.getBeanDefinition(beanName);
|
existingBeanDefinition = beanFactory.getBeanDefinition(beanName);
|
||||||
|
setQualifiedElement(existingBeanDefinition, handler);
|
||||||
}
|
}
|
||||||
else if (requireExistingBean) {
|
else if (requireExistingBean) {
|
||||||
Field field = handler.getField();
|
Field field = handler.getField();
|
||||||
@@ -448,10 +450,25 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
|
|||||||
private static RootBeanDefinition createPseudoBeanDefinition(BeanOverrideHandler handler) {
|
private static RootBeanDefinition createPseudoBeanDefinition(BeanOverrideHandler handler) {
|
||||||
RootBeanDefinition definition = new RootBeanDefinition(handler.getBeanType().resolve());
|
RootBeanDefinition definition = new RootBeanDefinition(handler.getBeanType().resolve());
|
||||||
definition.setTargetType(handler.getBeanType());
|
definition.setTargetType(handler.getBeanType());
|
||||||
definition.setQualifiedElement(handler.getField());
|
setQualifiedElement(definition, handler);
|
||||||
return definition;
|
return definition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the {@linkplain RootBeanDefinition#setQualifiedElement(java.lang.reflect.AnnotatedElement)
|
||||||
|
* qualified element} in the supplied {@link BeanDefinition} to the
|
||||||
|
* {@linkplain BeanOverrideHandler#getField() field} of the supplied
|
||||||
|
* {@code BeanOverrideHandler}.
|
||||||
|
* <p>This is necessary for proper autowiring candidate resolution.
|
||||||
|
* @since 6.2.6
|
||||||
|
*/
|
||||||
|
private static void setQualifiedElement(BeanDefinition beanDefinition, BeanOverrideHandler handler) {
|
||||||
|
Field field = handler.getField();
|
||||||
|
if (field != null && beanDefinition instanceof RootBeanDefinition rbd) {
|
||||||
|
rbd.setQualifiedElement(field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate that the {@link BeanDefinition} for the supplied bean name is suitable
|
* Validate that the {@link BeanDefinition} for the supplied bean name is suitable
|
||||||
* for being replaced by a bean override.
|
* for being replaced by a bean override.
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2024 the original author or authors.
|
* Copyright 2002-2025 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -147,8 +147,6 @@ class AotIntegrationTests extends AbstractAotTests {
|
|||||||
.filter(clazz -> clazz.getSimpleName().endsWith("Tests"))
|
.filter(clazz -> clazz.getSimpleName().endsWith("Tests"))
|
||||||
// TestNG EJB tests use @PersistenceContext which is not yet supported in tests in AOT mode.
|
// TestNG EJB tests use @PersistenceContext which is not yet supported in tests in AOT mode.
|
||||||
.filter(clazz -> !clazz.getPackageName().contains("testng.transaction.ejb"))
|
.filter(clazz -> !clazz.getPackageName().contains("testng.transaction.ejb"))
|
||||||
// Uncomment the following to disable Bean Override tests since they are not yet supported in AOT mode.
|
|
||||||
// .filter(clazz -> !clazz.getPackageName().contains("test.context.bean.override"))
|
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
// Optionally set failOnError flag to true to halt processing at the first failure.
|
// Optionally set failOnError flag to true to halt processing at the first failure.
|
||||||
@@ -169,7 +167,6 @@ class AotIntegrationTests extends AbstractAotTests {
|
|||||||
void endToEndTestsForSelectedTestClasses() {
|
void endToEndTestsForSelectedTestClasses() {
|
||||||
List<Class<?>> testClasses = List.of(
|
List<Class<?>> testClasses = List.of(
|
||||||
org.springframework.test.context.bean.override.easymock.EasyMockBeanIntegrationTests.class,
|
org.springframework.test.context.bean.override.easymock.EasyMockBeanIntegrationTests.class,
|
||||||
org.springframework.test.context.bean.override.mockito.MockitoBeanForByNameLookupIntegrationTests.class,
|
|
||||||
org.springframework.test.context.junit4.SpringJUnit4ClassRunnerAppCtxTests.class,
|
org.springframework.test.context.junit4.SpringJUnit4ClassRunnerAppCtxTests.class,
|
||||||
org.springframework.test.context.junit4.ParameterizedDependencyInjectionTests.class
|
org.springframework.test.context.junit4.ParameterizedDependencyInjectionTests.class
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2002-2025 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 java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.context.bean.override.example.ExampleService;
|
||||||
|
import org.springframework.test.context.bean.override.example.ExampleServiceCaller;
|
||||||
|
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.test.mockito.MockitoAssertions.assertIsMock;
|
||||||
|
import static org.springframework.test.mockito.MockitoAssertions.assertMockName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link MockitoBean @MockitoBean} where the mocked bean is associated
|
||||||
|
* with a custom {@link Qualifier @Qualifier} annotation and the bean to override
|
||||||
|
* is selected by name.
|
||||||
|
*
|
||||||
|
* @author Sam Brannen
|
||||||
|
* @since 6.2.6
|
||||||
|
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34646">gh-34646</a>
|
||||||
|
* @see MockitoBeanWithCustomQualifierAnnotationByTypeTests
|
||||||
|
*/
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
class MockitoBeanWithCustomQualifierAnnotationByNameTests {
|
||||||
|
|
||||||
|
@MockitoBean(name = "qualifiedService", enforceOverride = true)
|
||||||
|
@MyQualifier
|
||||||
|
ExampleService service;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ExampleServiceCaller caller;
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test(ApplicationContext context) {
|
||||||
|
assertIsMock(service);
|
||||||
|
assertMockName(service, "qualifiedService");
|
||||||
|
assertThat(service).isNotInstanceOf(QualifiedService.class);
|
||||||
|
|
||||||
|
// Since the 'service' field's type is ExampleService, the QualifiedService
|
||||||
|
// bean in the @Configuration class effectively gets removed from the context,
|
||||||
|
// or rather it never gets created because we register an ExampleService as
|
||||||
|
// a manual singleton in its place.
|
||||||
|
assertThat(context.getBeanNamesForType(QualifiedService.class)).isEmpty();
|
||||||
|
assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(1);
|
||||||
|
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(1);
|
||||||
|
|
||||||
|
when(service.greeting()).thenReturn("mock!");
|
||||||
|
assertThat(caller.sayGreeting()).isEqualTo("I say mock!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class Config {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
QualifiedService qualifiedService() {
|
||||||
|
return new QualifiedService();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
ExampleServiceCaller myServiceCaller(@MyQualifier ExampleService service) {
|
||||||
|
return new ExampleServiceCaller(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@interface MyQualifier {
|
||||||
|
}
|
||||||
|
|
||||||
|
@MyQualifier
|
||||||
|
static class QualifiedService implements ExampleService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String greeting() {
|
||||||
|
return "Qualified service";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2002-2025 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 java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.context.bean.override.example.ExampleService;
|
||||||
|
import org.springframework.test.context.bean.override.example.ExampleServiceCaller;
|
||||||
|
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.test.mockito.MockitoAssertions.assertIsMock;
|
||||||
|
import static org.springframework.test.mockito.MockitoAssertions.assertMockName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link MockitoBean @MockitoBean} where the mocked bean is associated
|
||||||
|
* with a custom {@link Qualifier @Qualifier} annotation and the bean to override
|
||||||
|
* is selected by type.
|
||||||
|
*
|
||||||
|
* @author Sam Brannen
|
||||||
|
* @since 6.2.6
|
||||||
|
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34646">gh-34646</a>
|
||||||
|
* @see MockitoBeanWithCustomQualifierAnnotationByNameTests
|
||||||
|
*/
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
class MockitoBeanWithCustomQualifierAnnotationByTypeTests {
|
||||||
|
|
||||||
|
@MockitoBean(enforceOverride = true)
|
||||||
|
@MyQualifier
|
||||||
|
ExampleService service;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ExampleServiceCaller caller;
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test(ApplicationContext context) {
|
||||||
|
assertIsMock(service);
|
||||||
|
assertMockName(service, "qualifiedService");
|
||||||
|
assertThat(service).isNotInstanceOf(QualifiedService.class);
|
||||||
|
|
||||||
|
// Since the 'service' field's type is ExampleService, the QualifiedService
|
||||||
|
// bean in the @Configuration class effectively gets removed from the context,
|
||||||
|
// or rather it never gets created because we register an ExampleService as
|
||||||
|
// a manual singleton in its place.
|
||||||
|
assertThat(context.getBeanNamesForType(QualifiedService.class)).isEmpty();
|
||||||
|
assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(1);
|
||||||
|
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(1);
|
||||||
|
|
||||||
|
when(service.greeting()).thenReturn("mock!");
|
||||||
|
assertThat(caller.sayGreeting()).isEqualTo("I say mock!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class Config {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
QualifiedService qualifiedService() {
|
||||||
|
return new QualifiedService();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
ExampleServiceCaller myServiceCaller(@MyQualifier ExampleService service) {
|
||||||
|
return new ExampleServiceCaller(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@interface MyQualifier {
|
||||||
|
}
|
||||||
|
|
||||||
|
@MyQualifier
|
||||||
|
static class QualifiedService implements ExampleService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String greeting() {
|
||||||
|
return "Qualified service";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2002-2025 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 java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.context.bean.override.example.ExampleService;
|
||||||
|
import org.springframework.test.context.bean.override.example.ExampleServiceCaller;
|
||||||
|
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy;
|
||||||
|
import static org.springframework.test.mockito.MockitoAssertions.assertMockName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link MockitoSpyBean @MockitoSpyBean} where the mocked bean is associated
|
||||||
|
* with a custom {@link Qualifier @Qualifier} annotation and the bean to override
|
||||||
|
* is selected by name.
|
||||||
|
*
|
||||||
|
* @author Sam Brannen
|
||||||
|
* @since 6.2.6
|
||||||
|
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34646">gh-34646</a>
|
||||||
|
* @see MockitoSpyBeanWithCustomQualifierAnnotationByTypeTests
|
||||||
|
*/
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
class MockitoSpyBeanWithCustomQualifierAnnotationByNameTests {
|
||||||
|
|
||||||
|
@MockitoSpyBean(name = "qualifiedService")
|
||||||
|
@MyQualifier
|
||||||
|
ExampleService service;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ExampleServiceCaller caller;
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test(ApplicationContext context) {
|
||||||
|
assertIsSpy(service);
|
||||||
|
assertMockName(service, "qualifiedService");
|
||||||
|
assertThat(service).isInstanceOf(QualifiedService.class);
|
||||||
|
|
||||||
|
assertThat(context.getBeanNamesForType(QualifiedService.class)).hasSize(1);
|
||||||
|
assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(1);
|
||||||
|
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(1);
|
||||||
|
|
||||||
|
when(service.greeting()).thenReturn("mock!");
|
||||||
|
assertThat(caller.sayGreeting()).isEqualTo("I say mock!");
|
||||||
|
verify(service).greeting();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class Config {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
QualifiedService qualifiedService() {
|
||||||
|
return new QualifiedService();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
ExampleServiceCaller myServiceCaller(@MyQualifier ExampleService service) {
|
||||||
|
return new ExampleServiceCaller(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@interface MyQualifier {
|
||||||
|
}
|
||||||
|
|
||||||
|
@MyQualifier
|
||||||
|
static class QualifiedService implements ExampleService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String greeting() {
|
||||||
|
return "Qualified service";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2002-2025 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 java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.test.context.bean.override.example.ExampleService;
|
||||||
|
import org.springframework.test.context.bean.override.example.ExampleServiceCaller;
|
||||||
|
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.test.mockito.MockitoAssertions.assertIsSpy;
|
||||||
|
import static org.springframework.test.mockito.MockitoAssertions.assertMockName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link MockitoSpyBean @MockitoSpyBean} where the mocked bean is associated
|
||||||
|
* with a custom {@link Qualifier @Qualifier} annotation and the bean to override
|
||||||
|
* is selected by name.
|
||||||
|
*
|
||||||
|
* @author Sam Brannen
|
||||||
|
* @since 6.2.6
|
||||||
|
* @see <a href="https://github.com/spring-projects/spring-framework/issues/34646">gh-34646</a>
|
||||||
|
* @see MockitoSpyBeanWithCustomQualifierAnnotationByNameTests
|
||||||
|
*/
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
class MockitoSpyBeanWithCustomQualifierAnnotationByTypeTests {
|
||||||
|
|
||||||
|
@MockitoSpyBean
|
||||||
|
@MyQualifier
|
||||||
|
ExampleService service;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ExampleServiceCaller caller;
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test(ApplicationContext context) {
|
||||||
|
assertIsSpy(service);
|
||||||
|
assertMockName(service, "qualifiedService");
|
||||||
|
assertThat(service).isInstanceOf(QualifiedService.class);
|
||||||
|
|
||||||
|
assertThat(context.getBeanNamesForType(QualifiedService.class)).hasSize(1);
|
||||||
|
assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(1);
|
||||||
|
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(1);
|
||||||
|
|
||||||
|
when(service.greeting()).thenReturn("mock!");
|
||||||
|
assertThat(caller.sayGreeting()).isEqualTo("I say mock!");
|
||||||
|
verify(service).greeting();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class Config {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
QualifiedService qualifiedService() {
|
||||||
|
return new QualifiedService();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
ExampleServiceCaller myServiceCaller(@MyQualifier ExampleService service) {
|
||||||
|
return new ExampleServiceCaller(service);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Qualifier
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@interface MyQualifier {
|
||||||
|
}
|
||||||
|
|
||||||
|
@MyQualifier
|
||||||
|
static class QualifiedService implements ExampleService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String greeting() {
|
||||||
|
return "Qualified service";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user