I am new to writing unit test cases in Java and I am trying to figure out how I should mock my test cases for my http client. I am trying to test the following function:
public HttpResponse getRequest(String uri) throws Exception {
String url = baseUrl + uri;
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet get = new HttpGet(url);
get.setHeader(AUTHORIZATION_HEADER, authorization);
get.setHeader(ACCEPT_HEADER, APPLICATION_JSON);
HttpResponse response = httpClient.execute(get);
return response;
}
I don't want to actually call the url and hit the server, I just want to try mock all the responses which I can get from the server, such as 500 or 200 or socket errors. I have looked into Mockito library to mock java functions, but I have read that Mockito cant be used for static methods.
Could someone guide me on how I should write a unit test for this? Also since httpClient is being created inside the function, is this a good practice for testing ?