Multiple levels of @Mock and @InjectMocks

14,783 views
Skip to first unread message

Collin Peters

unread,
Jun 16, 2011, 5:52:49 PM6/16/11
to moc...@googlegroups.com
I posted this on Stack Overflow but haven't gotten any traction there. Sorry for the double-post of sorts

http://stackoverflow.com/questions/6300439/multiple-levels-of-mock-and-injectmocks


So I understand that in Mockito @InjectMocks will inject anything that it can with the annotation of @Mock, but how to handle this scenario?

@Mock
private MockObject1 mockObject1;

@Mock
private MockObject2 mockObject2;

@InjectMocks
private SystemUnderTest systemUnderTest = new SystemUnderTest();

Imagine that MockObject2 has an attribute that is of type MockObject1, and SystemUnderTest has an attribute of type MockObject2. I would like to have mockObject1 injected into mockObject2, and mockObject2 injected into systemUnderTest.

Is this possible with annotations?

Szczepan Faber

unread,
Jun 17, 2011, 5:58:52 AM6/17/11
to moc...@googlegroups.com

Not possible at the moment. Interesting feature, though.

Cheers!
--
Szczepan Faber
Principal engineer@gradleware
Lead@mockito

> --
> You received this message because you are subscribed to the Google Groups "mockito" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/UW9S7JnpTisJ.
> To post to this group, send email to moc...@googlegroups.com.
> To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mockito?hl=en.
>

Geoffrey Wiseman

unread,
Jun 17, 2011, 9:17:07 AM6/17/11
to moc...@googlegroups.com
On Thu, Jun 16, 2011 at 5:52 PM, Collin Peters <collin...@gmail.com> wrote:
Imagine that MockObject2 has an attribute that is of type MockObject1, and SystemUnderTest has an attribute of type MockObject2. I would like to have mockObject1 injected into mockObject2, and mockObject2 injected into systemUnderTest.

Maybe I'm missing something here. Why would MockObject2 care if it has its dependency, MockObject1, injected into it? If you're mocking "2", then it presumably doesn't need "1". One of the primary reasons I use mocks is to test units of code in relative isolation rather than having to assemble a grid of dependent objects and have the test depend on all of them.

  - Geoffrey
--
Geoffrey Wiseman
http://www.geoffreywiseman.ca/

Szczepan Faber

unread,
Jun 17, 2011, 10:00:24 AM6/17/11
to moc...@googlegroups.com
Yeah, excellent question! I sort of assumed that the other @Mock is
not really a mock but a @Spy or... yet another SystemUnderTest (either
way... it feels like smell :)

Cheers!

> --
> You received this message because you are subscribed to the Google Groups
> "mockito" group.

> To post to this group, send email to moc...@googlegroups.com.
> To unsubscribe from this group, send email to
> mockito+u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mockito?hl=en.
>

--

Brice Dutheil

unread,
Jun 17, 2011, 10:52:51 AM6/17/11
to moc...@googlegroups.com
Actually you can almost do that with the combination of @Spy and @InjectMocks annotations


@Mock
private MockObject1 mockObject1;

@InjectMocks @Spy
private RealObject realObject = new RealObject();

@InjectMocks
private SystemUnderTest systemUnderTest = new SystemUnderTest();

Hope that helps




-- 
Bryce


--
You received this message because you are subscribed to the Google Groups "mockito" group.

Brice Dutheil

unread,
Jun 17, 2011, 10:54:47 AM6/17/11
to moc...@googlegroups.com
By the way, let alone the Demeter Law, it doesn't feel like testing in isolation and direct dependancies.
I wouldn't recommand designing code this way.

-- 
Bryce

Bill Zhang

unread,
Sep 26, 2013, 9:24:59 AM9/26/13
to moc...@googlegroups.com
That was a big help for me. Thanks a lot!
Reply all
Reply to author
Forward
0 new messages