Mockito测试RestClient aSync

时间:2016-11-10 22:09:02

标签: java unit-testing testing mockito android-testing

如何测试当前的方法?我无法达到像method1,method2等内部方法。我试图用Mockito测试不同测试中的所有内部方法。这可能吗?

@Override
public void myMethod(JSONObject json, final Listener listener) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    RestClientAPI.aSyncPost(url, request, contentType, new JsonHttpResponseHandler() {
        @Override
        public void onStart() {
            super.onStart();
            listener.method1();
        }
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            listener.method2(response);
        }
        @Override
        public void onFailure(int statusCode, Header[] headers, final String responseString, Throwable throwable) {
            listener.method3(responseString);
        }
        @Override
        public void onFinish() {
            super.onFinish();
            listener.method4();
        }
    });
}

1 个答案:

答案 0 :(得分:1)

下面的内容应该可以解决问题。

@Test
public void testMyMethodSuccess() {
  Listener mockListener = mock(Listener.class);
  myInstance.myMethod(jsonThatWillSucceed, mockListener);

  verify(mockListener, times(1)).method1();
  verify(mockListener, times(1)).method2(Mockito.eq(expectedResponse));
  verify(mockListener, times(1)).method4();
}