2017-05-09 1 views
0

Ich schrieb einige Code für zugehörige Datei mit Feder zu laden, es funktioniert gut, jetzt schreibe ich Integrationstestfälle für das, aber ich bin vor ein paar Ausgabe Mein Controller methddIntegration Testfall und Datei laden

@RequestMapping(value = "/{attributeName}/upload", method = RequestMethod.POST) 
@ResponseBody 
public Result uploadCompany(HttpServletRequest request, 
     @RequestParam MultipartFile file, @PathVariable String attributeName, 
     @RequestParam long dateKey) 
     throws IOException, PromotionException { 
    some code 
} 

Testfälle

@Test 
public void shouldReturnTrueStatusWhenUploadCompany() throws Exception { 
    MockMultipartFile file = new MockMultipartFile("company_upload", "company_upload.csv", 
      MediaType.MULTIPART_FORM_DATA_VALUE, EMPLOYEE_NUMBER_FILE_CONTENT.getBytes(UTF_8)); 
    mockMvc.perform(
      MockMvcRequestBuilders.fileUpload(
        PROMOTION + StringUtils.replace(ATTRIBUTE_NAME, "{attributeName}", "COMPANY") + "/upload") 
        .file(file).param("dateKey", "852017") .contentType(MediaType.MULTIPART_FORM_DATA) 
        .accept(MediaType.APPLICATION_JSON)) 
      .andExpect(status().isOk()); 

} 

aber ich bin immer

2017-05-09 13:42:42,506 ERROR [Test worker] INTERNAL_SERVER_ERROR: 
org.springframework.web.bind.MissingServletRequestParameterException: Required MultipartFile parameter 'file' is not present 

Wo ia Ich liege falsch?

+0

neue Änderung ", ....) zu neuer MockMultiPartFile (" file "," company_upload.csv ", ...) – pvpkiran

+0

Danke, Nein w es funktioniert. –

+0

kann es besser sein, wenn ich es als Antwort gebe, können Sie es als Antwort akzeptieren, so dass es für andere hilfreich ist, zu finden – pvpkiran

Antwort

4

Ihre Linie

MockMultipartFile file = new MockMultipartFile("company_upload", "company_upload.csv", 
        MediaType.MULTIPART_FORM_DATA_VALUE, EMPLOYEE_NUMBER_FILE_CONTENT.getBytes(UTF_8)); 

zu

MockMultipartFile file = new MockMultipartFile("file", "company_upload.csv", 
        MediaType.MULTIPART_FORM_DATA_VALUE, EMPLOYEE_NUMBER_FILE_CONTENT.getBytes(UTF_8)); 

oder ändern MockMultipartFile ("company_upload", „wie dieses etwas ändern company_upload.csv Ihre Controller-Methode Erklärung

public Result uploadCompany(HttpServletRequest request, 
       @RequestParam(value = "company_upload") MultipartFile file, @PathVariable String attributeName, 
       @RequestParam long dateKey)