I had the same problem with different values where some were parsed as BigDecimal
and some as double
.
So I choose not to use jsonPath, instead I convert the response to the actual object using MappingJackson2HttpMessageConverter
:
public class ControllerTest {
@Autowired
private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;
@SuppressWarnings("unchecked")
protected <T> T toObject(MockHttpServletResponse response, Class<T> clazz) throws IOException{
MockClientHttpResponse inputMessage = new MockClientHttpResponse(response.getContentAsByteArray(),
HttpStatus.valueOf(response.getStatus()));
return (T) mappingJackson2HttpMessageConverter.read(clazz, inputMessage);
}
@Test
public test(){
MvcResult result = mockMvc.perform(get("/rest/url")...)
.andExpect(status().isOk())
.andExpect(content().contentType(APPLICATION_JSON_UTF8))
.andReturn();
MyPojoClass pojo = toObject(result.getResponse(), MyPojoClass.class);
assertArrayEquals(new double[]{0.1, 0.2, 0.3}, pojo.getDoubleArray());
}
}