I am trying to cover below main class if (!Utility.isEmpty(errors)) block code coverage and i tried below code for adding errors in array List inside client class but still array showing as empty can some one help me to solve this issue.
Main class
public void processDiohShippingMethod(ShippingMethodRequest request) {
List<ResolvableErrorEnum> errors = new ArrayList<>();
checkoutRestClient.updateShippingMethod(request, errors);
if (!Utility.isEmpty(errors)) {
logger.info("ProcessDiohShippingMethod: Shipping method update failure from {} -> {}. Reverting. ",
existingSm, request.getShippingMethodType());
if (existingShippingMethod != null && Constants.RTG_ENJOY.equalsIgnoreCase(existingSm)) {
request.setStartTime(existingShippingMethod.getDropOffShippingInfo().getStartTime());
request.setEndTime(existingShippingMethod.getDropOffShippingInfo().getEndTime());
request.setScheduleType(existingShippingMethod.getDropOffShippingInfo().getCustomerScheduleType());
}
}
}
Client
public void updateShippingMethod(ShippingMethodRequest request, List<ResolvableErrorEnum> errors) {
String url = Utils.formatHttpUrl(url, Cart_Request);
try {
HttpEntity<?> entity = new HttpEntity<>(request);
JsonNode jsonNode = restTemplate.postForObject(url, entity, JsonNode.class);
if (!jsonNode.has(Constants.CONTENT)
|| !jsonNode.path(Constants.CONTENT).path(Constants.ERRORS).isMissingNode()) {
errors.add(ErrorMessages.SM_ERR_01);
return;
}
} catch (HttpClientErrorException e) {
errors.add(ErrorMessages.SM_ERR_01);
} catch (HttpServerErrorException e) {
errors.add(ErrorMessages.SM_ERR_01);
}
}
Test
@Test
public void test_processMethod() throws Exception {
Request req = new Request();
HttpEntity<?> entity = new HttpEntity<>(req);
String jsonResponse = "{\"error\":{\"errorId\":\"1234\",\"message\":\"error message\"}},\"status\":\"success\"}}";
ObjectMapper mapper = new ObjectMapper();
JsonNode actualObj = mapper.readTree(jsonResponse);
when(restTemplate.postForObject("url", entity, JsonNode.class)).thenReturn(actualObj);
ShippingMethodResponse response = helper.processDiohShippingMethod(req);
}