我正在使用Hamcrest CoreMatcher
类作为spring-test
集成测试的一部分.我的JSON看起来像:
{"data":[{"distanceInMiles":4,"id":"f97236ba-f4ef-4...
我的集成测试看起来像:
double miles = 4.0 Activity a = new BasicActivity(miles); this.activityManager.add(a); // A mock activity manager (in-memory) ... this.mockMvc.perform(get("/").accept("application/json")) .andExpect(jsonPath("$.data[0].distanceInMiles", is(miles)))
但是,断言失败了:
java.lang.AssertionError: JSON path "$.data[0].distanceInMiles" Expected: is <4.0> but: was <4> at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
我知道这里有一个单独的IsCloseTo
匹配器:http://hamcrest.org/JavaHamcrest/javadoc/1.3/org/hamcrest/number/IsCloseTo.html,但是像这样使用它:
.andExpect(jsonPath("$.data[0].distanceInMiles", closeTo(miles, 0)))
导致一个奇怪的错误:
java.lang.AssertionError: JSON path "$.data[0].distanceInMiles" Expected: a numeric value within <0.0> of <4.0> but: was a java.lang.Integer (<4>) at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)
我希望避免包含某种错误 - 我希望返回的值完全正确4
,我只是不关心包含多少尾随零.