如何在 Android Instrumentation Tests 中测试方法是否被调用?

How to test if a method has been invoked in Android Instrumentation Tests?

@Test
public void testWhenUserNameAndPasswordAreEnteredShouldAttemptLogin() throws Exception {
    LoginView loginView = Mockito.mock(LoginView.class);
    Mockito.when(loginView.getUserName()).thenReturn("George");
    Mockito.when(loginView.getPassword()).thenReturn("aaaaaa");
    loginPresenter.setLoginView(loginView);
    loginPresenter.onLoginClicked();
    Mockito.verify(loginPresenter).attemptLogin(loginView.getUserName(), loginView.getPassword());
}

这是我的测试,但由于 loginPresenter 是从 AndroidAnnotations 生成的 class 而它是 final,我不能对它进行 spy

那么有没有另一种方法(不一定使用mockito)来验证这个方法已经被调用?

PowerMock 让你模拟最终 类 和方法(和静态方法等)。