-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Bean created by @MockBean still has its fields autowired #6663
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
Comments
Thanks for the sample. I don't think this has anything to do with Rather than mocking I guess we could look at disabling autowiring for beans created using |
yes, but there's an any controller is interactes with the database throught service layer or whatever else. So, to test a controller it's neccessary to mock it's dependencies anyway. Data JPA repository shouldn't to be autowired at all since it's just a dependency of the dummy object. Will try your suggestion but IMAO, the |
In terms of how the mock is created, that's exactly what |
@wilkinsona , then it behaves rather like a stub that not always sufficient for the testing :( |
Looks like a rephrased clone of #6658 |
Yeah, @wilkinsona just told me. The complaints look alike :) |
Post-processing of mocked beans causes a number of problems: - The mock may be proxied for asynchronous processing which can cause problems when configuring expectations on a mock (spring-projectsgh-6573) - The mock may be proxied so that its return values can be cached or so that its methods can be transactional. This causes problems with verification of the expected calls to a mock (spring-projectsgh-6573, spring-projectsgh-5837) - If the mock is created from a class that uses field injection, the container will attempt to inject values into its fields. This causes problems if the mock is being created to avoid the use of one of those dependencies (spring-projectsgh-6663) - Proxying a mocked bean can lead to a JDK proxy being created (if proxyTargetClass=false) as the mock implements a Mockito interface. This can then cause injection failures as the types don’t match (spring-projectsgh-6405, spring-projectsgh-6665) All of these problems can be avoided if a mocked bean is not post-processed. Avoiding post-processing prevents proxies from being created and autowiring from being performed. This commit avoids post-processing by registering mocked beans as singletons rather than via a bean definition.
I experience this same problem when the unit under test's dependencies are plain old Spring beans that themselves have autowired dependencies. If the unit's dependencies are interfaces, no problem, they mock fine. But if classes, the mocking does not work completely and requires the dependencies of the dependency to be wired in. The "workaround" for this is to provide For me - someone trying to port a legacy (and very messy) Spring app over to several Spring Boot apps - getting this fixed is important. (Oh I see it is no longer waiting for triage - thanks!) |
Post-processing of mocked beans causes a number of problems: - The mock may be proxied for asynchronous processing which can cause problems when configuring expectations on a mock (gh-6573) - The mock may be proxied so that its return values can be cached or so that its methods can be transactional. This causes problems with verification of the expected calls to a mock (gh-6573, gh-5837) - If the mock is created from a class that uses field injection, the container will attempt to inject values into its fields. This causes problems if the mock is being created to avoid the use of one of those dependencies (gh-6663) - Proxying a mocked bean can lead to a JDK proxy being created (if proxyTargetClass=false) as the mock implements a Mockito interface. This can then cause injection failures as the types don’t match (gh-6405, gh-6665) All of these problems can be avoided if a mocked bean is not post-processed. Avoiding post-processing prevents proxies from being created and autowiring from being performed. This commit avoids post-processing by registering mocked beans as singletons as well as via a bean definition. The latter is still used by the context for type matching purposes. Closes gh-6573, gh-6663, gh-6664
Fixed in 0e00a49 |
I experience the same issue for |
@StasKolodyuk That's to be expected. Unlike a mock, a spy is a wrapper around a "proper" bean. For the bean that's being spied upon to work properly it needs to have its dependencies injected. |
@wilkinsona That makes sense, thank you! |
Just to say that i got rid of this bug by removing the There is no need for it anymore (using spring-boot 1.5.2) |
Uh oh!
There was an error while loading. Please reload this page.
The following code taken from the doc doesn't work as expected:
... where's the mocked bean is:
during test startup it fails:
Java: 1.8.0_65
Spring Boot: 1.4.0.RELEASE & 1.4.1-BUILD-SNAPSHOT
Maven: 3.3.3
Test project on GitHub
The text was updated successfully, but these errors were encountered: