【发布时间】:2015-08-09 13:36:11
【问题描述】:
这是我在控制器中的方法,由 @Controller 注释
@RequestMapping(value = "/getServerAlertFilters/{serverName}/", produces = "application/json; charset=utf-8")
@ResponseBody
public JSONObject getServerAlertFilters(@PathVariable String serverName) {
JSONObject json = new JSONObject();
List<FilterVO> filteredAlerts = alertFilterService.getAlertFilters(serverName, "");
JSONArray jsonArray = new JSONArray();
jsonArray.addAll(filteredAlerts);
json.put(SelfServiceConstants.DATA, jsonArray);
return json;
}
我期待 {"data":[{"useRegEx":"false","hosts":"v2v2v2"}]} 作为我的 json。
这是我的 JUnit 测试:
@Test
public final void testAlertFilterView() {
try {
MvcResult result = this.mockMvc.perform(get("/getServerAlertFilters/v2v2v2/").session(session)
.accept("application/json"))
.andDo(print()).andReturn();
String content = result.getResponse().getContentAsString();
LOG.info(content);
} catch (Exception e) {
e.printStackTrace();
}
}
这是控制台输出:
MockHttpServletResponse:
Status = 406
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
即使result.getResponse().getContentAsString() 也是一个空字符串。
有人可以建议如何在我的 JUnit 测试方法中获取我的 JSON,以便我可以完成我的测试用例。
【问题讨论】:
-
请注意,您收到了一个http错误代码406(不可接受的请求错误),这就是您的正文为空的原因
标签: java spring junit mocking spring-test-mvc