【发布时间】:2018-10-17 12:47:33
【问题描述】:
如何使用 JAVA 模拟 GET http 请求? 我有这个方法:
public HTTPResult get(String url) throws Exception{
try {
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
return new HTTPResult(response.getBody(), response.getStatusCode().value());
}
catch (ResourceAccessException e) {
String responseBody = e.getCause().getMessage();
JSONObject obj = new JSONObject(responseBody);
return new HTTPResult(obj.getString("responseBody"), Integer.parseInt(obj.getString("statusCode")));
}
}
我如何验证我得到的是:responseBody - 一些 json 和 statusCode,例如 200?
【问题讨论】:
-
你看
MockMvc了吗? -
我注意到您尚未接受任何答案。请考虑在某个时候这样做。或者,如果您有任何其他问题,请在此处告诉我。