2016-11-10 107 views
0

如何測試當前的方法?我不能達到像方法1,方法2等內部方法。我試圖在與Mockito不同的測試中測試所有內部方法。那可能嗎?Mockito測試RestClient aSync

@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

沿着下面的線應該做的伎倆。

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