JsonPathRequestMatchers 到 ResultMatcher 使用 MockMVC

Cha*_*lah 5 junit spring unit-testing jsonpath mockmvc

我正在使用 MockMVC 测试一个 todo 控制器:

            mockMvc.perform(MockMvcRequestBuilders.get("/toDos/")
                    .with(user("user").password("password").roles("ADMIN"))
                    .content("{ \"saved_date\": \"2010-01-01\"}")
                    .accept(MediaType.APPLICATION_JSON_VALUE))
                    .andExpect((ResultMatcher) jsonPath("$.id").doesNotExist())
                    .andExpect(status().isOk())
                    .andExpect( content().contentType("application/json"));
        }
Run Code Online (Sandbox Code Playgroud)

我不断收到此错误:

java.lang.ClassCastException: org.springframework.test.web.client.match.JsonPathRequestMatchers$5 cannot be cast to org.springframework.test.web.servlet.ResultMatcher
Run Code Online (Sandbox Code Playgroud)

我想删除对 (ResultMatcher) 的转换,但不知道如何创建一个 ResultMatcher 来测试 Id 的存在。有任何想法吗 ?

gly*_*ing 5

我想你想使用:

org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
Run Code Online (Sandbox Code Playgroud)

... 代替:

org.springframework.test.web.client.match.MockMvcResultMatchers.jsonPath
Run Code Online (Sandbox Code Playgroud)