spring - JsonPathRequestMatchers 到 ResultMatcher 使用 MockMVC

标签 spring unit-testing junit jsonpath mockmvc

我正在使用 MockMVC 测试一个 todo Controller :

            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"));
        }

我不断收到此错误:
java.lang.ClassCastException: org.springframework.test.web.client.match.JsonPathRequestMatchers$5 cannot be cast to org.springframework.test.web.servlet.ResultMatcher

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

最佳答案

我想你想使用:

org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath

... 代替:
org.springframework.test.web.client.match.MockMvcResultMatchers.jsonPath

关于spring - JsonPathRequestMatchers 到 ResultMatcher 使用 MockMVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44719251/

相关文章:

spring - 类在启动期间丢失但之后可用

java - Axon 3 未找到处理程序

java - 事务未启动 Spring + Hibernate + MySQL

java - Jetty 无法启动 war 文件

java - 使用私有(private)字段和@PostConstruct对Spring boot服务进行单元测试

java - 如何在 EasyMock 中验证方法调用并忽略返回值?

java - 为什么 junit 说我的 boolean 测试应该无效?

ruby-on-rails - 在 rails 上进行单元测试时,我得到路径的 "NameError: undefined local variable or method"- rails 3

unit-testing - 将模拟 DOM 事件传递给没有 jQuery 的指令处理程序

java - JUnit5 控制台启动器在提供的类路径中找不到任何测试