I am new to guice and injections. Need a help in writing the unit test.
I have a method that fetches a session token by making a GET request
String strTemp = "";
String sessionToken = "" ;
HttpResponse response;
try {
String url = String.format(URL_SESSION, email);
HttpGet request = new HttpGet(url);
<b>response = client.execute(request);</b>
} catch (Throwable e) {
LOG.error("Could not reach to the Server");
throw new ExecutionException(e, "Could not reach to the Server")
.withReason(Throwables.getRootCause(e).getMessage())
.withResolution("Check if the outbound Port is open and you can reach the Rapportive service");
}
In my unit test I wanna Inject the HttpClient to get a mock response. I have written a Mock Class
public abstract class MockHttpRequestBadQuery implements HttpClient { protected HttpResponse execute(HttpGet httpUriRequest) throws IOException { HttpResponse httpResponse = new BasicHttpResponse(new StatusLine() { @Override public ProtocolVersion getProtocolVersion() { return new ProtocolVersion("HTTP/1.1", 0, 0); } @Override public int getStatusCode() { return 400; } @Override public String getReasonPhrase() { return "Bad Request"; } }); HttpEntity entity = new StringEntity("{\"message\":\"\\n" + "SELECT badFieldName FROM Account\\n" + " ^\\n" + "ERROR at Row:1:Column:8\\n" + "No such column \'badFieldName\' on entity \'Account\'. If you are attempting to use" + " a custom field, be sure to append the \'__c\' after the custom field name. Please" + " reference your WSDL or the describe call for the appropriate names.\"," + " \"errorCode\":\"INVALID_FIELD\"}"); httpResponse.setEntity(entity); httpResponse.setHeader("Content-Type", "application/json;charset=UTF-8"); httpResponse.setHeader("Date", "Tue, 28 May 2013 16:06:21 GMT"); httpResponse.setHeader("Transfer-Encoding", "chunked"); return httpResponse; } }
I need help in how to inject so that whenever client.execute() is called, a mock response is generated.