Skip to content

Instantly share code, notes, and snippets.

@peekg
Created September 2, 2015 15:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peekg/61ddd7c7752b44861b3f to your computer and use it in GitHub Desktop.
Save peekg/61ddd7c7752b44861b3f to your computer and use it in GitHub Desktop.
Test class for mocking HttpClient requests
class MockRequestHandler extends RequestHandler {
private GetMethod mockGetMethod;
private HttpClient mockHttpClient;
public MockRequestHandler(int responseStatus,
byte[] responseBody) throws HttpException, IOException,
SecurityException, IllegalArgumentException,
NoSuchMethodException, IllegalAccessException,
InvocationTargetException {
mockGetMethod = Mockito.mock(GetMethod.class);
mockHttpClient = Mockito.mock(HttpClient.class);
Mockito.when(
mockHttpClient.executeMethod(Mockito.any(GetMethod.class)))
.thenReturn(responseStatus);
Mockito.when(mockGetMethod.getResponseBody()).thenReturn(
responseBody);
}
@Override
protected HttpClient getHttpClient() {
return mockHttpClient;
}
@Override
protected GetMethod getGetMethod() {
return mockGetMethod;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment