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

Test bean override support should only override singletons #33602

Closed
sbrannen opened this issue Sep 27, 2024 · 0 comments
Closed

Test bean override support should only override singletons #33602

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

Comments

@sbrannen
Copy link
Member

While working on #32933, I noticed some potential issues regarding singleton and scope semantics in our Bean Override support in spring-test, and I verified those assumptions in commit 9c74690.

  • If a FactoryBean signals it does not manage a singleton, the Bean Override support silently replaces it with a singleton.

  • An attempt to override a prototype-scoped bean or a bean configured with a custom scope results in one of the following.

    • If the bean type of the original bean definition is a concrete class, an attempt will be made to invoke the default constructor which will either succeed with undesirable results or fail with an exception if the bean type does not have a default constructor.
    • If the bean type of the original bean definition is an interface or a FactoryBean that claims to create a bean of a certain interface type, an attempt will be made to instantiate the interface which will always fail with a BeanCreationException.

In light of the above, we should rework the logic in BeanOverrideBeanFactoryPostProcessor.registerReplaceDefinition() so that only singleton beans can be overridden.

@sbrannen sbrannen added in: test Issues in the test module type: bug A general bug labels Sep 27, 2024
@sbrannen sbrannen added this to the 6.2.0-RC2 milestone Sep 27, 2024
@sbrannen sbrannen self-assigned this Sep 27, 2024
sbrannen added a commit that referenced this issue Oct 28, 2024
…mework

In gh-33602, we introduced strict singleton enforcement for bean
overrides -- for example, for @⁠MockitoBean, @⁠TestBean, etc. However,
the use of BeanFactory#isSingleton(beanName) can result in a
BeanCreationException for certain beans, such as a Spring Data JPA
FactoryBean for a JpaRepository.

In light of that, this commit relaxes the singleton enforcement in
BeanOverrideBeanFactoryPostProcessor by only checking the result of
BeanDefinition#isSingleton() for existing bean definitions.

This commit also updates the Javadoc and reference documentation to
reflect the status quo.

See gh-33602
Closes gh-33800
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