I have the following class,
@Service
public class ChannelIngester extends AbstractIngester<Long, Channel> {
private ChannelCategoryRepository channelCategoryRepository;
@Autowired
SystemService systemService;
@Autowired
public ChannelIngester(ChannelRepository repository,
ChannelCategoryRepository channelCategoryRepository) {
super(repository);
this.channelCategoryRepository = channelCategoryRepository;
}
And my test starts like this:
@RunWith(MockitoJUnitRunner.class)
public class ChannelIngesterTest {
@Mock ChannelCategoryRepository channelCategoryRepository;
@Mock ChannelRepository channelRepository;
@Mock SystemService systemService;
@InjectMocks ChannelIngester channelIngester;
I am using Mockito 1.9.0. The constructor injection is working fine
but the field injection is not.
The docs at
http://docs.mockito.googlecode.com/hg/1.9.0/org/mockito/InjectMocks.html
say:
Mockito will try to inject mocks only either by constructor injection,
setter injection, or property injection in order and as described
below. If any of the following strategy fail, then Mockito won't
report failure; i.e. you will have to provide dependencies yourself.
It seems like once it has done constructor injection it wont attempt
to do setter or property injection. The documentation seems to agree
as it says "only either by". Is there a way to make it do all?
Maybe it would be better if it first tried constructor injection and
failing that, tried setter injection, and then checked if there were
any properties that were still null that it could inject?
Tom