@InjectMocks
private AnotherClass anotherClass; // this class is autowired (using
Spring) with SomeClass!!!
@Before
public void initMocks() {
Mockito.mock(SomeClass.class, "someClass"); // on purpose placing
the mock method before initMocks method
MockitoAnnotations.initMocks(this); // initMocks should take care
of injecting the mock created in the line above
}
Another option would require a API change, for instance something
like:
@InjectMocks("someClass")
private AnotherClass anotherClass; // this class is @Autowired with
SomeClass!!!
or:
@InjectMocks(SomeClass.class)
private AnotherClass anotherClass; // this class is @Autowired with
SomeClass!!!
2 questions:
a) Are my findings correct?
b) What do you think about the ideas illustrated by the code snippets?
>However the @Mock annotation always requires you to define fields, but these are not always necessary
Hmmm, what do you mean by 'not necessary'? If the collaborator is not
necessary for the execution of the tested logic (SUT) then I think you
should change the SUT so that you don't need to pass that unnecessary
collaborator. If the collaborator is necessary but it's just a
'dummy', e.g. a mock without any interesting behavior then... well it
is still 'necessary' so it should be documented in the test, be it an
'unused' field ;)
Cheers!
Szczepan
> --
> 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.
>
>
--
Szczepan Faber
Principal engineer@gradleware
Lead@mockito
Mockito.doNothing().when(someClass).someMethod();
Thanks again for the comments.