如何使用mockmvc测试reuqestmethod.get的对象参数

yfjy0ee7  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(376)

如果我得到的方法定义如下

@GetMapping(value = "/getfood")

public Food getFood(@valid final Order order)

如何在mockmvc测试中用以下代码传递order对象

this.mockMvc.perform(get("/getfood"))

谢谢

oxf4rvwz

oxf4rvwz1#

您可以使用requestbuilder和MVC结果:

RequestBuilder request = MockMvcRequestBuilders.get("/getfood").accept(MediaType.HTML);

MvcResult result = mockMvc.perform(request).andReturn();

CustomResponse customResponse = new CustomResponse("your data");

ResponseEntity response = new ResponseEntity<CustomResponse>(customResponse, HttpStatus.OK);

assertEquals(response.getBody().toString(), response.getResponse().getContentAsString());

您需要根据您的代码调整此示例。customresponse是您需要创建的类。其余的是从org.springframework.test.web.servlet导入的*

相关问题