如何模拟@InjectMocks类的方法?
2022-08-31 11:08:52
例如,我有处理程序:
@Component
public class MyHandler {
@AutoWired
private MyDependency myDependency;
public int someMethod() {
...
return anotherMethod();
}
public int anotherMethod() {...}
}
为了测试它,我想写这样的东西:
@RunWith(MockitoJUnitRunner.class}
class MyHandlerTest {
@InjectMocks
private MyHandler myHandler;
@Mock
private MyDependency myDependency;
@Test
public void testSomeMethod() {
when(myHandler.anotherMethod()).thenReturn(1);
assertEquals(myHandler.someMethod() == 1);
}
}
但实际上,每当我试图嘲笑它时,它就会打电话。我该怎么做才能嘲笑它的方法?anotherMethod()
myHandler