Provide complete support for qualifier annotations with Bean Overrides
Prior to this commit, the Test Bean Override feature provided support for overriding beans based on qualifier annotations in several scenarios; however, qualifier annotations got lost if they were declared on the return type of the @Bean method for the bean being overridden and the @BeanOverride (such as @MockitoBean) was based on a supertype of that return type. To address that, this commit sets the @BeanOverride field as the "qualified element" in the RootBeanDefinition to ensure that qualifier annotations are available for subsequent autowiring candidate resolution. Closes gh-34646
This commit is contained in:
@@ -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