IntoTheDeep
IntoTheDeep

Reputation: 4118

Mockito test RestClient aSync

How to test current method? I cannot reach inner methods like method1, method2.. etc. I am trying to test all inner methods in different test with Mockito. Is that possible?

@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();
        }
    });
}

Upvotes: 0

Views: 99

Answers (1)

Jan
Jan

Reputation: 4369

Something along the lines of below should do the trick.

@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();
}

Upvotes: 1

Related Questions