0
@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());
}

This is my test, but as loginPresenter is a class generated from AndroidAnnotations and it is final, I cannot spy on it.

So is there another way (not necessarily using mockito) to verify that this method has been invoked?

Kaloyan Roussev
  • 14,515
  • 21
  • 98
  • 180

1 Answers1

1

PowerMock lets you mock final classes and methods (and static methods, etc).

joseca
  • 307
  • 2
  • 8
  • How do I integrate PowerMock with my existing code? – Kaloyan Roussev Mar 18 '16 at 09:59
  • 1
    The documentation is well explained https://github.com/jayway/powermock/wiki/MockFinal. Basically, you must annotate your test classes with @RunWith(PowerMockRunner.class) and @PrepareForTest(MyFinalClass.class). Then mock its behavious like in mockito: `MyFinalClass mock = createMock(MyFinalClass.class); expect(mock.myMethod()).andReturn(whatever);` – joseca Mar 18 '16 at 10:06
  • Thanks, I just read the docs but I couldnt find a way to use PowerMock in Android Studio.. only maven and some jars. Pretty laconic – Kaloyan Roussev Mar 18 '16 at 10:10
  • btw isn't there any other way? some assertion or reflection by Junit or something? – Kaloyan Roussev Mar 18 '16 at 10:10
  • To use it in Android Studio, I just put in the build.gradle, into dependencies: `testCompile ('org.powermock:powermock-module-junit4:1.6.3') { exclude module: 'hamcrest-core' exclude module: 'objenesis' }`. I don't know how to do it by reflection. – joseca Mar 18 '16 at 10:15
  • Thanks I will try that. Hopefully it will work with `androidTestCompile` – Kaloyan Roussev Mar 18 '16 at 10:18
  • Should this `junit4:1.6.3'` match my junit version? – Kaloyan Roussev Mar 18 '16 at 10:19
  • No. It's the PowerMock version. The current one I think is 1.6.4 – joseca Mar 18 '16 at 10:20