Como verificar o JSON no corpo da resposta com mockMvc
Este é o meu método dentro do meu controlador, que é anotado por@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;
}
Estou esperando{"data":[{"useRegEx":"false","hosts":"v2v2v2"}]}
como meu json.
E este é o meu teste 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();
}
}
Aqui está a saída do console:
MockHttpServletResponse:
Status = 406
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
Atéresult.getResponse().getContentAsString()
é uma string vazia.
Alguém pode sugerir como obter meu JSON no meu método de teste JUnit para que eu possa concluir meu caso de teste.