Honor @⁠Primary before fallback qualifier for Bean Overrides

Prior to this commit, test bean overrides (for example, @⁠MockitoBean,
@⁠TestBean, etc.) eagerly honored the name of the annotated field as a
fallback qualifier, effectively ignoring @⁠Primary and @⁠Fallback
semantics for certain use cases.

This led to situations where a bean override for a test would select a
different bean than the core container would for the same autowiring
metadata.

To address that, this commit revises the implementation of
BeanOverrideBeanFactoryPostProcessor so that @⁠Primary and @⁠Fallback
semantics are consistently honored before attempting to use the
annotated field's name as a fallback qualifier.

Closes gh-34374
This commit is contained in:
Sam Brannen
2025-02-07 17:59:08 +01:00
parent 1d7cb4fc13
commit 9107f7b592
4 changed files with 169 additions and 37 deletions

View File

@@ -0,0 +1,100 @@
/*
* 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 org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.mockito.BDDMockito.then;
import static org.springframework.test.mockito.MockitoAssertions.assertIsMock;
import static org.springframework.test.mockito.MockitoAssertions.assertIsNotMock;
/**
* Tests that {@link MockitoBean @MockitoBean} can be used to mock a bean when
* there are multiple candidates; one is primary; and the field name matches
* the name of a candidate which is not the primary candidate.
*
* @author Sam Brannen
* @since 6.2.3
* @see MockitoBeanWithMultipleExistingBeansAndOnePrimaryIntegrationTests
* @see MockitoBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests
* @see MockitoBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests
*/
@ExtendWith(SpringExtension.class)
class MockitoBeanWithMultipleExistingBeansAndOnePrimaryAndOneConflictingQualifierIntegrationTests {
// The name of this field must be "baseService" to match the name of the non-primary candidate.
@MockitoBean
BaseService baseService;
@Autowired
Client client;
@Test // gh-34374
void test(ApplicationContext context) {
assertIsMock(baseService, "baseService field");
assertIsMock(context.getBean("extendedService"), "extendedService bean");
assertIsNotMock(context.getBean("baseService"), "baseService bean");
client.callService();
then(baseService).should().doSomething();
}
@Configuration(proxyBeanMethods = false)
@Import({ BaseService.class, ExtendedService.class, Client.class })
static class Config {
}
@Component("baseService")
static class BaseService {
public void doSomething() {
}
}
@Primary
@Component("extendedService")
static class ExtendedService extends BaseService {
}
@Component("client")
static class Client {
private final BaseService baseService;
public Client(BaseService baseService) {
this.baseService = baseService;
}
public void callService() {
this.baseService.doSomething();
}
}
}

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@ import static org.springframework.test.mockito.MockitoAssertions.assertMockName;
* @author Sam Brannen
* @author Phillip Webb
* @since 6.2
* @see MockitoBeanWithMultipleExistingBeansAndOnePrimaryAndOneConflictingQualifierIntegrationTests
* @see MockitoBeanWithMultipleExistingBeansAndExplicitBeanNameIntegrationTests
* @see MockitoBeanWithMultipleExistingBeansAndExplicitQualifierIntegrationTests
*/

View File

@@ -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");
* you may not use this file except in compliance with the License.
@@ -37,6 +37,14 @@ public abstract class MockitoAssertions {
assertThat(isMock(obj)).as("%s is a Mockito mock", message).isTrue();
}
public static void assertIsNotMock(Object obj) {
assertThat(isMock(obj)).as("is a Mockito mock").isFalse();
}
public static void assertIsNotMock(Object obj, String message) {
assertThat(isSpy(obj)).as("%s is a Mockito mock", message).isFalse();
}
public static void assertIsSpy(Object obj) {
assertThat(isSpy(obj)).as("is a Mockito spy").isTrue();
}