如何使用Mockito验证空隙方法



我正在为其名称为 NetworkRequest的类编写一些unitTest。在此类中,我有一个函数,其名称为 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(时创建一个测试为null。

我写了以下类似的东西:

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'

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

相关内容

  • 没有找到相关文章

最新更新