I try to do the following test:
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class SomeClassTest {
// needs to be mocked and injected
@Mock
private SomeDao someDao;
@InjectMocks
private SomeClass someClass = new someClass();
@Test
void test() {
when(someDao.retrieveSomeData).thenReturn(10);
}
}
This the simplified class where the object I want to inject is instantiated and used in a method:
public class SomeClass {
private void someMethod() {
SomeDao someDao = new SomeDao();
someDao.retrievSomeData();
}
}
But I can also write the class as following with the instantiation in the constructor:
public class SomeClass{
SomeDao someDao;
public SomeClass() {
someDao = new SomeDao();
}
private void someMethod() {
someDao.retrievSomeData();
}
}
Result:
It is still the real function with the reals object someDao.rerieveSomeData()
called and not my mocked object! Why? I am rather confused.