Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identical Bean Overrides are silently allowed #34054

Closed
sbrannen opened this issue Dec 8, 2024 · 0 comments
Closed

Identical Bean Overrides are silently allowed #34054

sbrannen opened this issue Dec 8, 2024 · 0 comments
Assignees
Labels
in: test Issues in the test module type: bug A general bug
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Dec 8, 2024

Overview

While working on #34025, I noticed that the Bean Overrides feature in Spring Framework behaves differently than Spring Boot's @MockBean/@SpyBean support with regard to identical overrides.

Specifically, Spring Boot's DefinitionsParser preemptively rejects "identical" overrides and throws an IllegalStateException to signal the configuration error to the user.

https://github.com/spring-projects/spring-boot/blob/51c992593fa17804f1432e4da2f939ee48e9b481/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/DefinitionsParser.java#L108-L113

Whereas, the Bean Override feature silently allows an identical bean override to override a previous bean override, which can be seen in the following test classes.

@MockitoSpyBean("field1")
ExampleService field;
@MockitoSpyBean("field1")
ExampleService renamed1;

Here, the @MockitoSpyBean("field") declarations on field and renamed1 are identical and should be rejected. However, the second bean override declared for the renamed1 field actually "overrides the override" for the field field, resulting in a single spied bean in the ApplicationContext. Granted, one would logically only expect one such spied bean, but multiple attempts to spy on the same bean should be rejected since there is no reason to "override an override" with a duplicate, identical override.

The scenario in MockitoBeanDuplicateTypeIntegrationTests is similar, but the outcome is different.

Here, the two effectively identical @MockitoBean declarations result in two mock beans being created, since the ApplicationContext does not originally contain any bean of type ExampleService.

Proposal

To align with the behavior of @MockBean and @SpyBean in Spring Boot regarding the rejection of identical overrides, and to help developers avoid scenarios that are potentially confusing or difficult to debug, we should also reject identical bean overrides in the Spring TestContext Framework.

Related Issues

@sbrannen sbrannen added in: test Issues in the test module type: bug A general bug labels Dec 8, 2024
@sbrannen sbrannen added this to the 6.2.1 milestone Dec 8, 2024
@sbrannen sbrannen self-assigned this Dec 8, 2024
@sbrannen sbrannen changed the title Duplicate Bean Overrides should be rejected Duplicate Bean Overrides are silently allowed Dec 8, 2024
@sbrannen sbrannen changed the title Duplicate Bean Overrides are silently allowed Identical Bean Overrides are silently allowed Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: bug A general bug
Projects
None yet
Development

No branches or pull requests

1 participant