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?


Solution 1:

Since I didn't get any response here I asked on the Mockito forums. Here is a link to the discussion: https://groups.google.com/d/topic/mockito/hWwcI5UHFi0/discussion

To summarize the answers, technically this would kind of defeat the purpose of mocking. You should really only mock the objects needed by the SystemUnderTest class. Mocking things within objects that are themselves mocks is kind of pointless.

If you really wanted to do it, @spy can help

Solution 2:

It is possible by combining @Spy with @InjectMocks. For your example, it would be:

@Spy
private MockObject1 mockObject1 = new MockObject1 ();

@Spy @InjectMocks //if MockObject2 has a MockObject1, then it will be injected here.
private MockObject2 mockObject2 = new MockObject2 ();

@InjectMocks
private SystemUnderTest systemUnderTest;

Solution 3:

Other solution I found is using java sintax instead annotation to make the @Spy object injected.

@Spy
private MockObject1 mockObject1 = new MockObject1 ();

@InjectMocks //if MockObject2 has a MockObject1, then it will be injected here.
private MockObject2 mockObject2 = spy(MockObject2.class);

@InjectMocks
private SystemUnderTest systemUnderTest;