对于不同的值,我遇到了同样的问题,其中一些被解析为BigDecimal
,一些被解析为double
.
所以我选择不使用 jsonPath,而是使用以下命令将响应转换为实际对象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());
}
}