mercredi 2 septembre 2020

Spring write Unit Test to Upload File for Rest Controller

I'm trying to write test case for my Rest Controller

    @Test
    public void anonymousUploadControllerTest() throws Exception {
        MockMultipartFile mockMultipartFile = new MockMultipartFile("file", "test-file.txt", "text/plain","sample data1".getBytes());


        mockMvc.perform(MockMvcRequestBuilders.multipart("/upload")
                .file(mockMultipartFile))
                .andExpect(MockMvcResultMatchers.status().is(200))
                .andExpect((ResultMatcher) content().string("File has been uploaded"));


    }

It return:

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /upload
       Parameters = {}
          Headers = [Content-Type:"multipart/form-data;charset=UTF-8"]
             Body = null
    Session Attrs = {}

Handler:
             Type = org.sharefiles.root.controllers.UploadController
           Method = org.sharefiles.root.controllers.UploadController#uploadFile(MultipartFile)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = null

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 500
    Error message = null
          Headers = [Content-Type:"application/json", X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
     Content type = application/json
             Body = {"httpStatus":"INTERNAL_SERVER_ERROR","responseMessage":"Something went wrong with uploading your file!","fileAccessLink":null,"authenticated":false}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: Response status 
Expected :200
Actual   :500

Could you help me to find where is mistake ? When I wrote for Service then everything worked fine.

There is full code on github: https://github.com/group-project-keeper/share-files-project/blob/master/src/test/java/org/sharefiles/root/uploadTests/uploadTest.java

Greeting and thanks for help if something.

Aucun commentaire:

Enregistrer un commentaire