-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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 |
Hi @sbrannen - question on the new annotations: This was useful for test cases which wanted to stub out some set of beans (ie to avoid some side effect of regular instantiation), but didn't care about specifying behavior. Is that feature intentionally dropped? Or is it just not-yet-added? Is there discussion on this somewhere? |
Hi @nmck257,
If you are referring to the ability to declare If you want to mock a bean by type, you can declare Does that answer your questions? Cheers, Sam |
@sbrannen - yes, I see how that can work, though I think the pattern has drawbacks. Suppose you have multiple Subjectively, I also think that if the test developer's intent is not to define any behavior for the mocked bean, then it's easier to read and maintain if we can avoid adding an unused field to the class scope. |
Thank for the feedback, @nmck257. I understand your concerns, but supporting However, feel free to create a new GitHub issue to discuss the merits or adding support for that. I cannot promise that we will implement it, but we are open to discussing it. For what it's worth, someone else just raised this issue in the Spring Boot issue tracker as well. Regards, Sam |
Address newly deprecated code issues. Address ambiguous `any()` issue that now appears and breaks tests. The `@MockBean` is no longer available and we must instead use `@MockitoBean`. see: spring-projects/spring-framework#29917 (comment) see: spring-projects/spring-framework#33925 see: https://docs.spring.io/spring-framework/reference/testing/annotations/integration-spring/annotation-mockitobean.html
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: