java - 在 Junit mockMVC 中模拟空对象
问题描述
我正在尝试将 null 作为 JUnit mockMvc 的内容类型传递,但它失败了
Object obj =null;
MvcResult result = mockMvc.perform(post("/mgr/v1/processEvent")
.contentType(MediaType.APPLICATION_JSON)
.content(String.valueOf(obj))
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().is4xxClientError()).andReturn();
它不会进入控制器调用
请帮忙
解决方案
不要使用String.valueOf(null);
. Null 被解释为导致NullpointerException
. 只需使用空字符串或留下完整的行。
MvcResult result = mockMvc.perform(post("/mgr/v1/processEvent")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().is4xxClientError()).andReturn();
推荐阅读
- kubernetes - 错误:升级失败:替换对象失败:服务“api”无效:spec.clusterIP:无效值:“”:字段不可变
- java - 使用后如何删除一行代码?
- javascript - 尝试制作:使用不和谐 js 做出反应以赋予角色
- python - 使用networkx查找三角形的总数
- java - 为什么我的反向 LinkedList 方法不能多次工作?
- python - Python:嵌套循环映射字段一一
- python - 如何迭代地搜索结构?
- blockchain - 挖矿节点如何获得相同的目标?
- istio - 如何为 Istio Egress 配置 SNI 直通
- javascript - 将 Python 后端与桌面应用程序的 HTML/CSS/JS 用户界面集成?