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 safe way to override and mock beans in the TestContext framework #29917
Comments
We think this is an interesting idea, but something that could be implemented in Spring Framework rather than Spring Boot. We'll transfer the issue for the Framework team to consider. |
Or introduce an option to define which bean names or classes can be overridden. spring.main.allow-bean-definition-overriding-names=myBean and then you can override only a bean with the name |
Unfortunately, the code snippet you've provided wouldn't work. A test class can't be a
I am not sure how that would look like, but it means we have to instantiate the test instance before refreshing the context (since the bean factory can instantiate that bean at any point in time). Brainstorming with @simonbasle, we're considering several options. First an infrastructure that is inspired by the class MyTest {
@MethodOverride
private MyBean myBean;
static MyBean myBeanValueSupplier() {
return new MyBean();
} Where Does that make sense? Any feedback on the proposal? |
Please note that this has been addressed by @simonbasle in commit e1bbdf0 which:
|
The new |
Overriding beans in testing is commonly needed yet current approach is unnecessarily complicated and at the same time limited and very error prone (allow-bean-definition-overriding will hide other issues). For example see spring-projects/spring-boot#30513
It would be great if there was a single annotation that allowed overriding single bean in testing, e.g.:
Even better if it could be relaxed to support field instead of method.
The text was updated successfully, but these errors were encountered: