I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the
Since 1.8.3 Mockito has @InjectMocks
- this is incredibly useful. My JUnit tests are @RunWith
the MockitoJUnitRunner
and I build @Mock
objects that satisfy all the dependencies for the class being tested, which are all injected when the private member is annotated with @InjectMocks
.
I @RunWith
the SpringJUnit4Runner
for integration tests only now.
I will note that it does not seem to be able to inject List
in the same manner as Spring. It looks only for a Mock object that satisfies the List
, and will not inject a list of Mock objects. The workaround for me was to use a @Spy
against a manually instantiated list, and manually .add the mock object(s) to that list for unit testing. Maybe that was intentional, because it certainly forced me to pay close attention to what was being mocked together.