0

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'code': was expecting ('true', 'false' or 'null') at [Source: [B@5ff2e84b; line: 1, column: 9]

I;m getting this error while running the mockito test case..

please find the below code for more info.

  @Test
public void testFindLearningStandardItemCodeById() throws Exception{
    String id = "2411";
    String json = JsonUtil.getInstance().writeValueAsString(id);
    String learningStandardItemCode = new String("code");

    when(learningStandardItemManager.findCodeById(anyString())).thenReturn(learningStandardItemCode);

    ResultActions resultActions = mockMvc.perform(post("/standards/jurisdiction/standard/item/findCodeById")
            .accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).content(json.getBytes())
    );

    resultActions.andExpect(status().isOk());
    final MvcResult mvcResult = resultActions.andReturn();
    String contents = mvcResult.getResponse().getContentAsString();

    JsonResponse response = JsonUtil.getInstance().getObjectFromJsonResponse(contents, JsonResponse.class);
    System.out.print(response);
    assertNotNull(response);
}
ram
  • 49
  • 4

1 Answers1

0

Thanks Mandar DharurKar,

But I found the solution, we just need to pass only String and not parsed string. e.g. accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).content(id)

call content(String str) instead of content(byte[] content)& when we retriving response at that time call getObjectFromResponse() method instead of getObjectFromJsonResponse().

e.g. String response = JsonUtil.getInstance().getObjectFromResponse(contents, String.class);

it works for me..!

ram
  • 49
  • 4