Mockito multiple levels injectMocks problem

516 views
Skip to first unread message

赵冬

unread,
May 8, 2014, 11:23:13 AM5/8/14
to moc...@googlegroups.com
Hello Mockito groups,

Currently I met a problem with mockito multiple level injectMocks problem. I create a github demo project to better illustrate the problem.

The problem summary is:

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.

-- 

Regards
Dong Zhao

Brice Dutheil

unread,
Jul 24, 2014, 2:33:42 PM7/24/14
to moc...@googlegroups.com

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.

Reply all
Reply to author
Forward
0 new messages