MockMvc 测试未到达 RestController 中多部分文件的端点
MockMvc Test does not get to the endpoint for a Multipart file in a RestController
我在订单控制器中调用一个服务,该控制器接收一个多部分文件并对其进行处理并将其保存到数据库中。我正在尝试为其创建一个 Spring Rest Doc,但它甚至没有达到终点。我正在创建服务期望的订单列表。如图所示,它以流的形式接收订单,并在将其保存到数据库之前转换为订单流。我已经展示了控制器的主要部分和生成其余文档的代码。当我 运行 代码出现以下异常时,它甚至在我设置断点时都没有到达端点。我也使用了 fileupload() 但这也不起作用。
例外情况是:
内容类型 = application/json
正文 = {"path":"/orders/order_reception","exceptionName":
"MissingServletRequestPartException","message":"必需的请求部分 'uploadFile' 不是
现在",
"rootExceptionName":"MissingServletRequestPartException",
"rootMessage":"MissingServletRequestPartException: Required request part 'uploadFile' is not present"}
@RestController
@RequestMapping(value = "/orders")
@Validated
class OrderController{
@PostMapping(path = "/order_reception")
public ResponseEntity receiveData(@RequestPart MultipartFile uploadFile,
HttpServletRequest request,
HttpServletResponse response) {
if (!uploadFile.isEmpty()) {
try {
Reader reader = new InputStreamReader(request.getInputStream()));
... save file
return new ResponseEntity<>(HttpStatus.HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
@Test
public void sendData() throws Exception {
ObjectMapper mapper = new ObjectMapper();
Order order = repository.getOrder("1233333");
List<Order> orderList = new ArrayList<>():
resourceList.add(order);
MockMultipartFile orderFile = new MockMultipartFile("order-data", "order.json", "application/json",
mapper.writeValueAsString(orderList).getBytes(Charset.defaultCharset()));
mockMvc.perform(multipart("/orders/order_reception")
.file(orderFile))
.andExpect(status().isCreated())
.andDo(document("send-order",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
}
谢谢 Marten Deinum,您关于文件名错误的建议已修复。
我只是在 MockMultipartFile( "uploadsFile", ...)
中更改了名称
我在订单控制器中调用一个服务,该控制器接收一个多部分文件并对其进行处理并将其保存到数据库中。我正在尝试为其创建一个 Spring Rest Doc,但它甚至没有达到终点。我正在创建服务期望的订单列表。如图所示,它以流的形式接收订单,并在将其保存到数据库之前转换为订单流。我已经展示了控制器的主要部分和生成其余文档的代码。当我 运行 代码出现以下异常时,它甚至在我设置断点时都没有到达端点。我也使用了 fileupload() 但这也不起作用。
例外情况是:
内容类型 = application/json 正文 = {"path":"/orders/order_reception","exceptionName": "MissingServletRequestPartException","message":"必需的请求部分 'uploadFile' 不是
现在", "rootExceptionName":"MissingServletRequestPartException", "rootMessage":"MissingServletRequestPartException: Required request part 'uploadFile' is not present"}
@RestController
@RequestMapping(value = "/orders")
@Validated
class OrderController{
@PostMapping(path = "/order_reception")
public ResponseEntity receiveData(@RequestPart MultipartFile uploadFile,
HttpServletRequest request,
HttpServletResponse response) {
if (!uploadFile.isEmpty()) {
try {
Reader reader = new InputStreamReader(request.getInputStream()));
... save file
return new ResponseEntity<>(HttpStatus.HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
return new ResponseEntity(HttpStatus.BAD_REQUEST);
}
@Test
public void sendData() throws Exception {
ObjectMapper mapper = new ObjectMapper();
Order order = repository.getOrder("1233333");
List<Order> orderList = new ArrayList<>():
resourceList.add(order);
MockMultipartFile orderFile = new MockMultipartFile("order-data", "order.json", "application/json",
mapper.writeValueAsString(orderList).getBytes(Charset.defaultCharset()));
mockMvc.perform(multipart("/orders/order_reception")
.file(orderFile))
.andExpect(status().isCreated())
.andDo(document("send-order",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint())));
}
谢谢 Marten Deinum,您关于文件名错误的建议已修复。 我只是在 MockMultipartFile( "uploadsFile", ...)
中更改了名称