Re: [mockito] how to mock httpclient.execute method

2,974 views
Skip to first unread message

Eric Lefevre-Ardant

unread,
May 28, 2013, 6:16:28 AM5/28/13
to moc...@googlegroups.com
How and where are you instantiating serviceimpl and, most important, how are you passing the mocked values to it?


On 28 May 2013 11:54, remala sabareesh <rd.sab...@gmail.com> wrote:
Hi,

I am facing some issues while mocking httpclient

@Mock
    HttpClientFactoryBean httpClientFactoryBean;
    @Mock
    HttpClient mockHttpClient;
    @Mock
    ResponseHandler<String> mockResponseHandler;
    @Mock
    DefaultHttpClient defaultHttpClient;
@Test
public void testhttp(){

         String responseBody = "";
        String jsonstring = "{\"id\":\"iCw0ZdSK23\",\"lastName\":\"Sultana\",\"firstName\":\"Nazima\"}";
        HttpResponse response = prepareResponse(200, responseBody);
      
         HttpGet httpget = new HttpGet("www.google.com");
         httpClientFactoryBean = Mockito.mock(HttpClientFactoryBean.class);               
         defaultHttpClient = Mockito.mock(DefaultHttpClient.class);
         mockHttpClient = Mockito.mock(HttpClient.class);
         mockResponseHandler = Mockito.mock(BasicResponseHandler.class);
         Mockito.when(httpClientFactoryBean.getHttpClient()).thenReturn(defaultHttpClient);       
         Mockito.when(mockHttpClient.execute(httpget)).thenReturn(response);
         Mockito.when(mockResponseHandler.handleResponse(response)).thenReturn(jsonstring);
         Map<String, List<UserUpdate>> updatesMap = serviceimpl.getLatestUpdates("100000a", "asdfasdfasdfsdafd");
         System.out.println("map response"+updatesMap);
         Assert.assertNotNull(updatesMap);
}
when i called serviceimpl getlatestupdates method

getLatestUpdates(String,String){
httpget = new HttpGet(url.toString());
ResponseHandler<String> responseHandler = new BasicResponseHandler();
httpClient = httpClientFactory.getHttpClient();
HttpResponse response = httpClient.execute(httpget); // this is hitting to the URL and giving the response it is not taking the MOCKED VALUES
responseBody = responseHandler.handleResponse(response);

}

please give me the solution

Thanks & Regards,
Sabareesh.

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+u...@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at http://groups.google.com/group/mockito?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages