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

Provide a way to create custom ApplicationContextFactory in SpringBootContextLoader #38205

Closed
FyiurAmron opened this issue Nov 3, 2023 · 2 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@FyiurAmron
Copy link

FyiurAmron commented Nov 3, 2023

As it currently stands, SpringBootContextLoader creates the contexts using the 0-arg c-tors, which in turn use the this.beanFactory = new DefaultListableBeanFactory(); from GenericApplicationContext; beanFactory is private and final, so as a result there is basically no simple way to inject a custom BeanFactory to the test context (or at least there is none that I am aware of).

While it might seem that it should be enough for the custom BeanFactory to be tested in isolation, sometimes it's the integration with any conforming GenericApplicationContext subclass itself that needs to be tested (as it is in my case e.g.). Obviously, it's still possible to provide a completely custom context to tests, but for that to integrate well and to adhere to POLA it requires rewriting/copy-pasting sizeable parts of SpringBootContextLoader - while e.g. a simple annotation or annotation param to e.g. @SpringBootTest allowing to provide a class name of the custom BeanFactory would make this trivial, and shouldn't require too much additional code in SpringBootContextLoader.

And yes, this is a rare use case, but still I believe there is nothing to be lost by providing this functionality somehow, and in general having more flexibility in testing envs is always beneficial IMO. I do understand that this might get the lowest-than-low priority though, so while I'm no SB expert I can try doing a PR for this if it gets greenlit (guidelines welcome in that case), just to see how it works out.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 3, 2023
@philwebb
Copy link
Member

philwebb commented Nov 6, 2023

I'm not too keen on adding a new attribute to @SpringBootTest but I wonder if we could make SpringBootContextLoader easier to subclass.

@FyiurAmron If the existing getApplicationContextFactory method was protected would that meet your needs?

@philwebb philwebb added the status: waiting-for-feedback We need additional information before we can continue label Nov 6, 2023
@FyiurAmron
Copy link
Author

@philwebb yes, I think it would do the trick; I completely understand your objections here, so I guess that just having an option to override that single method via protected while keeping the rest intact would be a reasonable solution - that'd be just a couple of lines of easily-manageable subclass implementation instead of a ~600 lines of copy-paste needed to be kept in sync with upstream :D

@philwebb philwebb changed the title provide a way to inject custom BeanFactory in SpringBootContextLoader Provide a way to inject custom BeanFactory in SpringBootContextLoader Nov 7, 2023
@philwebb philwebb added type: enhancement A general enhancement and removed status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged labels Nov 7, 2023
@philwebb philwebb self-assigned this Nov 7, 2023
@philwebb philwebb added this to the 3.2.x milestone Nov 7, 2023
@philwebb philwebb changed the title Provide a way to inject custom BeanFactory in SpringBootContextLoader Provide a way to create custom ApplicationContextFactory in SpringBootContextLoader Nov 7, 2023
@philwebb philwebb modified the milestones: 3.2.x, 3.2.0 Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants