BreenDeen
BreenDeen

Reputation: 722

MockMvc Test does not get to the endpoint for a Multipart file in a RestController

I am calling a service in an orders controller which receives a multipart file and processes it and saving it into a database. I am trying to create a Spring Rest Doc for it but it is not even hitting the endpoint. I am creating a list of orders which is what the service expects. It receives the order as a stream as shown and converts into a stream of orders before saving it into a database. I have shown the main part of the controller and my code for generating the rest docs. When I run the code I get the following exception, it never even hits the endpoint when I set a breakpoint. I also used fileupload() but that did not work either.

Exception is:

Content type = application/json Body = {"path":"/orders/order_reception","exceptionName": "MissingServletRequestPartException","message":"Required request part 'uploadFile' is not

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

Upvotes: 0

Views: 1260

Answers (1)

BreenDeen
BreenDeen

Reputation: 722

Thank you Marten Deinum, your suggestion that the file name was wrong fixed it. I simply changed name in the MockMultipartFile( "uploadsFile", ...)

Upvotes: 1

Related Questions