0

I have a method which returns data from a third party rest service. I don't have access to third party service from Local machine. I tried to implement MockRestServiceServer and WireMock to test the same but not successful.

Below is the code

public String callRestClient() throws IOException {
    RestTemplate restTemplate = new RestTemplate();
    Map<String, String> reqMap = new HashMap<String, String>();
    reqMap.put(----, ----);
    try {
        reqMap.put(----, -----);
        JSONObject reqJSON = new JSONObject(reqMap);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entityH = new HttpEntity<String>(reqJSON.toString());
        ResponseEntity<String> result = restTemplate.exchange(vaultUri, HttpMethod.POST, entityH, String.class);        
        JSONParser parser = new JSONParser();
        JSONObject resultJsonObj;
        resultJsonObj = (JSONObject) parser.parse(result.getBody());
        JSONObject authObj = (JSONObject) resultJsonObj.get("auth");
        String client_token = (String) authObj.get("client_token");
        return client_token;
    } catch (ParseException e) {
        log.error(e.getMessage());
    }
    return null;
}

Application is being developed using spring boot. Thanks in Advance.

raj
  • 1
  • 1
  • Try to wrap service call with a ProxyService and mock ProxyService methods, and test your flow after that. Unit testing should only tests "units" which also means you should test your specific business cases, do not test http request, or response, this responsibility belongs to automation/contract testings. – Emre Savcı Jul 12 '18 at 08:02
  • Thanks @Emre, But I am trying to understand is there any way I can test this code by changing the method as well. Can you please give me more on ProxyService mocking. I am not discussing on responsibilities. – raj Jul 12 '18 at 14:00

0 Answers0