I am using mockito to test the call to an Interface, but I get some problems when I want to verify that the interface method 'goToLoginInterface()' was called consecutively when I call to 'goToLogin()'. It is supposed to be something simple but I've been trying to find a solution for hours. I put and assert to verify that 'getActivityParent()' is effectively returning the mock Interface object, and it is!, so I don't know what the problem is.
public class LoginSimpleFragment extends Fragment {
private ActivityInterface mParentActivity;
public interface ActivityInterface {
void goToLoginInterface();
}
public ActivityInterface getActivityInterface(){
return mParentActivity;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.login_simple, container, false);
}
public void goToLogin() {
getActivityInterface().goToLoginInterface();
}
}
This is my test class
@Config(manifest = "../app/src/main/AndroidManifest.xml", emulateSdk = 18)
@RunWith(RobolectricTestRunner.class) // <== REQUIRED for Robolectric!
public class TestLoginActivity {
@Test
public void testPositiveButtonAction() throws Exception {
LoginSimpleFragment mockLoginSampleFragment =
mock(LoginSimpleFragment.class);
LoginSimpleFragment.ActivityInterface mockInterface =
mock(LoginSimpleFragment.ActivityInterface.class);
Mockito.doNothing().when(mockInterface).goToLoginInterface();
//doReturn(mockInterface).when(mockLoginSampleFragment).getActivityInterface();
when(mockLoginSampleFragment.getActivityInterface()).thenReturn(mockInterface);
mockLoginSampleFragment.goToLogin();
assert( Mockito.mockingDetails(mockLoginSampleFragment.getActivityInterface()).isMock() );
verify(mockInterface).goToLoginInterface();
}
}
the output test said:
Wanted but not invoked:
activityInterface.goToLoginInterface();
-> at co.mobico.mainactivities.TestLoginActivity.testPositiveButtonAction(TestLoginActivity.java:35)
Actually, there were zero interactions with this mock.
TestLoginActivity.java:35 is the line 'verify(mockInterface).goToLoginInterface()', at the end of test function
Can you helpme to make the test pass?, I'm using TDD in Android with robolectric, so if I cannot get solve it, I cannot continue working, Thanks!