### Question:
FileController extends AbstractWebController<E, D, S> { @RequestMapping(value = "/upload.action", method = RequestMethod.POST) public ResponseEntity<?> uploadFile( @RequestParam("file") MultipartFile uploadedFile) { logger.debug("Requested to upload a multipart-file"); Map<String, Object> responseMap = null; try { E file = service.uploadFile(uploadedFile); logger.info("Successfully uploaded file " + file.getFileName()); responseMap = ResultSet.success(file); } catch (Exception e) { logger.error("Could not upload the file: " + e.getMessage()); responseMap = ResultSet.error("Could not upload the file: " + e.getMessage()); } final HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); return new ResponseEntity<>(responseMap, responseHeaders, HttpStatus.OK); } @SuppressWarnings("unchecked")  FileController(); protected  FileController(Class<E> entityClass); @Override @Autowired @Qualifier("fileService") void setService(S service); @RequestMapping(value = "/upload.action", method = RequestMethod.POST) ResponseEntity<?> uploadFile(
        @RequestParam("file") MultipartFile uploadedFile); @RequestMapping(value = "/get.action", method = RequestMethod.GET) ResponseEntity<?> getFile(@RequestParam Integer id);  }### Answer:
@Test public void uploadFile_shouldWorkAsExpected() throws Exception { byte[] mockupBytes = "Just some mockup bytes".getBytes(); final String fileName = "fileName.txt"; final String fileType = "text/plain"; MockMultipartFile mockMultipartFile = new MockMultipartFile( "file", fileName, fileType, mockupBytes); File uploadedFile = new File(); uploadedFile.setFile(mockupBytes); uploadedFile.setFileName(fileName); uploadedFile.setFileType(fileType); when(fileServiceMock.uploadFile(mockMultipartFile)).thenReturn(uploadedFile); mockMvc.perform( MockMvcRequestBuilders.fileUpload("/file/upload.action").file(mockMultipartFile)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$.*", hasSize(3))) .andExpect(jsonPath("$.success", is(true))) .andExpect(jsonPath("$.total", is(1))) .andExpect(jsonPath("$.data.fileName", is(fileName))) .andExpect(jsonPath("$.data.fileType", is(fileType))); verify(fileServiceMock, times(1)).uploadFile(mockMultipartFile); verifyNoMoreInteractions(fileServiceMock); }
@Test public void uploadFile_shouldReturnErrorMessageIfUploadIsEmpty() throws Exception { final String errorMessage = "Upload failed. File is null."; byte[] content = null; MockMultipartFile file = new MockMultipartFile("file", content); doThrow(new Exception(errorMessage)) .when(fileServiceMock).uploadFile(file); mockMvc.perform( MockMvcRequestBuilders.fileUpload("/file/upload.action").file(file)) .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$.*", hasSize(2))) .andExpect(jsonPath("$.success", is(false))) .andExpect(jsonPath("$.message", containsString(errorMessage))); verify(fileServiceMock, times(1)).uploadFile(file); verifyNoMoreInteractions(fileServiceMock); }