In my project unit test, I need to test the various components managed under spring context. Some components have multiple levels dependant on others. I try to use Mockito to mock the ones that performs the DAO and outside store cache functionalities. I use the @Mock, @Spy and @InjectMocks annoations to accomplish this. But the weird thing is that I need to run the below code snippet twice in order to make the autowired field work corrent.
MockitoAnnotations.initMocks(this);
I am not sure whether this is a bug or an unspoorted feature for Mockito. So I create this demo project to better illustrate this problem.
You can refer the detailed information in the link:
https://github.com/zd987/MockitoDemo
Expect your replies. Thanks very much.
--
Hi Dong Zhao,
Sorry for the late reply. First a project on github to show your problem, waw. congrat, it’s the very best way :)
SHowever, I’m sorry but Mockito isn’t able to inject a graph of dependency. It is no his role. Mocks should serve to test in isolation a single piece of code and the mockito injecter is only here to help with boiler plate code when injecting a single layer of dependencies.
Injecting a graph would require a real DI framework whick mockito isn’t and never will be.
When I’m faced with this problem, I usually refactor the production and test code in order to test in isolation.
HTH
Cheers,
— Brice
--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito.
For more options, visit https://groups.google.com/d/optout.