I have a class (PriceSetter) that I'm testing with Mockito, and the class has an internal dependency (a database). I want to mock this internal dependency and then inject it into the class, but the dependency is not specified in my constructor. Thus, Mockito automatically tries to do constructor injection and the dependency never gets injected.
I tried using @Mock on my database object and @InjectMocks on my PriceSetter class, but Mockito automatically calls the constructor, and it fails to inject my database mock as the database is not passed into the constructor.
class PriceSetter {
private Table priceTable;
public PriceSetter(Dependency d1, Dependency d2) {
this.d1 = d1;
this.d2 = d2;
}
}
@RunWith(MockitoJUnitRunner.class)
class PriceSetterTest{
@InjectMocks
private PriceSetter setter;
@Mock Table priceTable;
@Mock Dependency d1;
@Mock Dependency d2;
@Test
public void someTestMethod() {
when(priceTable.getItem(any())).thenReturn(Specified item);
setter.priceTable.getItem("item"); -> Doesn't return item specified by mocked behavior
}
}
I expect priceTable
to be injected, but it isn't injected. Only d1 and d2 are injected through constructor injection.