【问题标题】:default type used by jayway jsonpath?jayway jsonpath 使用的默认类型?
【发布时间】:2013-08-02 12:10:05
【问题描述】:

当我有一个值时

x = 0.5771622052130299

我想使用 spring 3.2 Resutlmatcher 执行以下操作:

.andExpect(jsonPath("$.[1].myX").value(myPojo.getMyX()))

其中 myPojo.getMyX 返回一个双精度,测试失败,因为 json 被转换为 BigDecimal,并带有错误消息

java.lang.AssertionError: 
For JSON path $.[1].myX type of value expected:
<class java.lang.Double> but was:<class java.math.BigDecimal>

我怎样才能避免这种情况?

【问题讨论】:

    标签: java json unit-testing spring-mvc jsonpath


    【解决方案1】:

    因为它需要一个大十进制...您可以将双精度转换为大十进制

    .andExpect(jsonPath("$.[1].myX", is(new BigDecimal(myPojo.getMyX()))))
    

    【讨论】:

      【解决方案2】:

      使用 Hamcrest 创建一个转换为 BigDecimal 的自定义匹配器。这是一个教程:

      unrelated question 的代码也可能有所帮助。

      参考文献

      【讨论】:

        【解决方案3】:

        我对不同的值有同样的问题,其中一些被解析为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());
            }
        
        }
        

        【讨论】:

          【解决方案4】:

          我遇到了同样的问题,但我无法更改 Hamcrest 用于 JSON 值 (BigDecimal) 的类型。

          使用此解决方法:

          public static final double DEFAULT_PRECISION = 0.000001;
          
          public static Matcher<BigDecimal> closeTo(double value, double precision) {
              return BigDecimalCloseTo.closeTo(new BigDecimal(value), new BigDecimal(precision));
          }
          
          public static Matcher<BigDecimal> closeTo(double value) {
              return closeTo(value, DEFAULT_PRECISION);
          }
          

          ...

          .andExpect(jsonPath("$.values.temperature").value(closeTo(-13.26517)));
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-12-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多