如何使用Mockito验证void方法

时间:2019-06-26 07:01:44

标签: android unit-testing static mockito

我正在为名称为unitTest的类写一些NetworkRequest。 在此类中,我有一个函数,其名称为run

public void run(NetworkResponseListener listener) {
        if (listener == null)
            return;

        this.mListener = listener;

        CheckInternetAccess.checkInternetAccess(manager, new NetworkStateListener() {
            @Override
            public void onInternetResponse(int internetCode) {

                if (internetCode == ErrorCodes.CODE_HAS_INTERNET_ACCESS) {
                    runRequest();
                } else {
                    mListener.onNetworkResponse(null);
                }

            }
        });

    }

我想在输入侦听器(NetworkResponseListener)时创建测试 为空。

我写了如下内容:

public class NetworkRequestTest {

    private ReceivedRequest receivedRequest = Mockito.mock(ReceivedRequest.class);
    private ConnectivityManager connectivityManager = Mockito.mock(ConnectivityManager.class);
    private NetworkStateListener networkStateListener = Mockito.mock(NetworkStateListener.class);

    @Test
    public void checkInternetAccessShouldNotCallWithNullListener() {
        NetworkRequest networkRequest = new NetworkRequest(receivedRequest, connectivityManager);//SUT

        networkRequest.run(null);

        Mockito.verify(CheckInternetAccess.checkInternetAccess(connectivityManager, networkStateListener), Mockito.never());
    }
}

我要检查CheckInternetAccess.checkInternetAccess的验证 但我看到一条错误消息:

Wrong 1st argument type. Found: 'void', required: 'java.lang.Void'

1 个答案:

答案 0 :(得分:0)

尝试在模拟对象上使用Mockito.verifyZeroInteractions(...)。