[英]How to test spring's @RequestBody which recieves custom object using MockMvc
我正在尝试测试我的控制器方法,该方法将输入参数作为应用程序中的对象之一。我知道如何使用String作为输入参数,而不使用定制对象进行测试。
下面是我的应用程序中的代码
@RequestMapping(value = "/someUrl", method = RequestMethod.POST)
public ResponseEntity<?> save(@RequestBody Transaction transaction) {
我以为下面的测试代码是测试的方法之一,但是失败了
@Test
public void test() throws Exception {
Transaction Transaction = new Transaction();
Gson gson = new Gson();
String json = gson.toJson(transaction);
mockMvc.perform(post("/someUrl")
.contentType(MediaType.APPLICATION_JSON)
.content(json));
当我运行上面的测试时,我收到错误消息:“ 实际上,该模拟的交互为零 ”。
您能帮我如何将自定义对象传递给MockMvc post方法。
mapper.writeValueAsString(json)
是一种更好的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.