简体   繁体   中英

How verify the call to an interface with Mockito?

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!

You are lost in a maze of mocks.

You're not actually using a LoginSampleFragment , you're using a Mock of that class. So when you call goToLogin(), nothing happens, because the mock won't run your normal class code.

Even if you would instruct your mock to do something when you call goToLogin() , at this point you aren't testing your code anymore, you're just testing your own mock setup, spinning in circles.

This might be a good reading: When should I mock?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM