-
Notifications
You must be signed in to change notification settings - Fork 38.3k
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
Spring Test support for @MockBean
or similar [SPR-14083]
#18655
Comments
Michael Huffman commented I do not believe this is a Spring issue. Mockito's |
Martin Meyer commented Has anyone looked back at this problem in the past two years? This problem persists and from a user perspective it really seems like I shouldn't have to go so far out of my way to inject mocks. Mockito has been under much heavier development lately, and with all that's changed in JUnit 5 and Spring Framework 5 maybe fixing this problem might be easier than it was when I originally filed the issue..? |
Bulk closing outdated, unresolved issues. Please, reopen if still relevant. |
This is definitely still relevant. It has tripped up other developers on almost every project I've worked on involving Spring Framework. We always get into a state early on where unit tests aren't passing and we can't figure out why, and it always turns out it's because injecting mocks on something being proxied by cglib doesn't work as expected. Once we re-realize the problem, we start using We really could use a better system to do this for us. This shouldn't be so hard and manual. Even discovering that this is a problem isn't that easy. |
@elreydetodo Have you seen the |
A similar proxy issue was raised in Spring Boot spring-projects/spring-boot#6871 with |
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed. |
Actually it looks like that would resolve the problem. I'm reading a little into that issue and the Javadoc of Can we get that annotation and the test execution listener promoted to spring-test? |
Please note that I'm not using Spring Boot, so I can't simply use that annotation and be done with it. Spring Boot doesn't seem like the appropriate place for such fundamental functionality to live. |
It's been about a year since it was suggested to move the MockBean annotation (and its affiliated functionality) from the spring-boot project to the spring-test project. What's involved in completing that move? |
@MockBean
or similar [SPR-14083]
Superseded by #29917 which introduces support for |
Martin Meyer opened SPR-14083 and commented
If I have a Transaction Manager running in unit tests, it becomes impossible for Mockito to properly initialize and inject mocks. This problem seems to be related to usage of proxy classes, not specific to using a transaction manager.
Here's a basic example of a test class:
What I see in the debugger is that on the
someRealBean.doSomething()
line,someRealBean
contains a mock reference to myMockBean
instance. But when I set the next breakpoint withinRealBean#doSomething()
and go there, the local field within the object is not a mock one, it's a real@Autowire
'd bean.In real terms, the impact on me is that tests fail whenever I try to implement a TransactionManager and have it configured during tests. I end up trying to call DAO methods that should have been mocked, and it tries to issue database calls on a DataSource that isn't really ready to service them.
I think the problem is a race between Mockito and Spring to performing their setup. I think the solution is for
SpringJUnit4ClassRunner
to automagically call the equivalent ofMockitoAnnotations.initMocks(this)
iforg.mockito.Mockito
is in the classpath. This would mean adding an optional dependency on Mockito.There was an issue opened against Mockito for this, which was closed as WONTFIX. They suggested that it be fixed in Spring instead of bringing in a Spring dependency there.
The obvious workaround here is to not enable anything that will do AOP proxying during your unit tests.
Affects: 4.2.5
The text was updated successfully, but these errors were encountered: