Hamcrest jsonPath 错误地双倍失败
Harmcrest jsonPath wrongly failing with double
我有以下 REST 端点,returns 一个简单的数据 class 有两个双打
@GetMapping("/test")
public LatLng test() {
return new LatLng(-26.733229893125923, -26.733229893125923);
}
我的测试是这样的:
mockMvc.perform(
get("/test")
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect((jsonPath("$.latitude", is(-26.733229893125923))))
.andExpect((jsonPath("$.longitude", is(-26.733229893125923))));
测试总是失败
java.lang.AssertionError: JSON path "$.latitude" Expected: is
<-26.733229893125923>
but: was <-26.733229893125923>
堆栈跟踪正确地显示它们是相同的值,但由于某种原因测试仍然失败。
如果我将双精度减一,则测试有效。如果我使用 Matchers.closeTo(-26.733229893125923, 0.01)
测试也会失败
The stacktrace correctly shows them being the same values but the test still failed for some reason.
这实际上表明它们的字符串表示形式(即对对象调用 toString()
的结果)是相同的。它没有表明对象在 .equals()
语义方面是相等的。
因此,expected
对象很可能是 Double
;而 actual
对象可能是 Float
.
如果是这样的话,下面的内容应该可以使您的测试通过。
.andExpect(jsonPath("$.latitude", is(-26.733229893125923f)))
如果您使用的是 Spring 4.3.15 或更新版本,您应该也可以使用以下版本。
.andExpect(jsonPath("$.latitude").value(is(-26.733229893125923), Double.class))
我有以下 REST 端点,returns 一个简单的数据 class 有两个双打
@GetMapping("/test")
public LatLng test() {
return new LatLng(-26.733229893125923, -26.733229893125923);
}
我的测试是这样的:
mockMvc.perform(
get("/test")
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect((jsonPath("$.latitude", is(-26.733229893125923))))
.andExpect((jsonPath("$.longitude", is(-26.733229893125923))));
测试总是失败
java.lang.AssertionError: JSON path "$.latitude" Expected: is <-26.733229893125923> but: was <-26.733229893125923>
堆栈跟踪正确地显示它们是相同的值,但由于某种原因测试仍然失败。
如果我将双精度减一,则测试有效。如果我使用 Matchers.closeTo(-26.733229893125923, 0.01)
The stacktrace correctly shows them being the same values but the test still failed for some reason.
这实际上表明它们的字符串表示形式(即对对象调用 toString()
的结果)是相同的。它没有表明对象在 .equals()
语义方面是相等的。
因此,expected
对象很可能是 Double
;而 actual
对象可能是 Float
.
如果是这样的话,下面的内容应该可以使您的测试通过。
.andExpect(jsonPath("$.latitude", is(-26.733229893125923f)))
如果您使用的是 Spring 4.3.15 或更新版本,您应该也可以使用以下版本。
.andExpect(jsonPath("$.latitude").value(is(-26.733229893125923), Double.class))