我在util类中有一个方法,它返回一个Application实例。
private static Application application;
public static Application getApplication() {
return application;
}
现在,我需要模仿这个方法,我用以下方式做了。我正在使用Mockito框架。
@Mock
private Application application;
@Test
public void testMethod(){
Utils utils = mock(Utils.class);
when(utils.getApplication()).thenReturn(application);
}
但是,这会抛弃异常
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
我应该在代码中进行哪些更改以使其有效?