3

我正在使用 MockMvcResultMatchers 来测试我的控制器类。

这是一个示例代码

        RequestBuilder request = get("/employee/")
            .contentType(MediaType.APPLICATION_JSON)
            .accept(APPLICATION_JSON_UTF8);

        mockMvc
            .perform(request)
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.total").exists());

但是我如何将 $.total 值与数字进行比较?

我的意思是,有没有办法找出 $.total > 0?

4

2 回答 2

8

json 路径value方法可以org.hamcrest.Matcher作为参数。所以你可以使用GreaterThan类:

jsonPath("['key']").value(new GreaterThan(1))

这个类来自org.mockito.internal.matchers包。

于 2015-08-25T08:59:57.843 回答
5

您可以将GreaterThan( )方法与hasSize() 一起使用。

例如,您应该执行以下操作:

.andExpect(jsonPath("$.result", hasSize(greaterThan(0))))

您可以org.hamcrest.Matchers这里查看其他类方法。

于 2020-10-16T07:50:11.083 回答