0

I am not able to get how to call the controller method(API) in this case. How to send the MultipartFile as request parameter and how to pass the HttpServletRequest so that mockMvc can call the actual method for testing.

@RunWith(SpringRunner.class)
public class PartnerSiteLoadControllerTest {

private MockMvc mockMvc;

@Mock
private PartnerSiteUploadService partnerSiteUploadService;

@InjectMocks
private PartnerSiteLoadController partnerSiteLoadController;

@Before
public void setup() {
    this.mockMvc = MockMvcBuilders.standaloneSetup(partnerSiteLoadController)
            .setControllerAdvice(new PartnerExceptionHandlerMapper()).build();
}

@Test
public void uploadSitesInBulk()throws Exception {
    String userId = "userId";
    HttpServletRequest request = mock(HttpServletRequest.class);
    UserPrincipal userPrincipal = new UserPrincipal();
    userPrincipal.setId("id");

    BulkUploadResponseDTO bulkUploadResponseDTO = new BulkUploadResponseDTO();

    FileInputStream inputFile = new FileInputStream( "src/test/resources/PartnerSites_2019-09-04_v1.xlsx");
    MockMultipartFile file = new MockMultipartFile("PartnerSites_2019-09-04_v1.xlsx", "PartnerSites_2019-09-04_v1.xlsx", "multipart/form-data", inputFile);
    when(file.getOriginalFilename()).thenReturn("PartnerSites_2019-09-04_v1.xlsx");
    when(request.getAttribute("userPrincipal")).thenReturn(userPrincipal);
    when(partnerSiteUploadService.uploadSitesInBulk(userId,file)).thenReturn(bulkUploadResponseDTO);

    mockMvc.perform(MockMvcRequestBuilders.fileUpload("/v4/slm/partners/sites/import")
            .file(file).contentType(MediaType.MULTIPART_FORM_DATA).requestAttr("userPrincipal",userPrincipal))
            .andExpect(status().isOk());

    verify(partnerSiteUploadService,times(1)).uploadSitesInBulk(userId,file);
    verifyNoMoreInteractions(partnerSiteUploadService);


}

} Controller class method

@RestController
@RequestMapping("/v4/slm/partners/sites/import")
@Api(value = "Site Bulk Upload Service")
@Slf4j
@Validated
public class PartnerSiteLoadController {

private PartnerSiteUploadService partnerSiteUploadService;

@Autowired
public PartnerSiteLoadController(PartnerSiteUploadService partnerSiteUploadService) {
    this.partnerSiteUploadService = partnerSiteUploadService;
}

@PostMapping(value = "", headers = ("content-type=multipart/*"))
@ApiOperation(value = "Import sites in bulk")
public ResponseEntity<BulkUploadResponseDTO> uploadSitesInBulk(@RequestParam("file") MultipartFile excelFile, HttpServletRequest request){

    UserPrincipal userPrincipal = (UserPrincipal) request.getAttribute("userPrincipal");
    String userId = userPrincipal.getId();

    log.info("Received excel file with name {}......",excelFile.getOriginalFilename());

    if(!excelFile.isEmpty()){
        return ResponseEntity.status(HttpStatus.CREATED).body(partnerSiteUploadService.uploadSitesInBulk(userId,excelFile));
    }
    else{
        throw new BadRequestException("Received empty excel file");
    }
}

}

while executing the test I am getting the 400 error code. the mockmvc is not calling the original API.

  • Does this answer your question? [Spring MockMVC inject mockHttpServletRequest when not in method signature](https://stackoverflow.com/questions/18486691/spring-mockmvc-inject-mockhttpservletrequest-when-not-in-method-signature) – pvpkiran Apr 23 '20 at 13:58
  • I tried that also but it is not resolving my problem as I have to pass MultiPartFile and HttpServletRequest in the mockMvc call and I am not getting a way for that. It is not call in the original method returning 400 status code. –  Apr 23 '20 at 14:20
  • mockMvc.perform(fileUpload("/v4/slm/partners/sites/import").file(file).with(new RequestPostProcessor() { public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) { request.setAttribute("userPrincipal", userPrincipal); return request; }}) .contentType(MediaType.MULTIPART_FORM_DATA)) .andExpect(status().isOk()); this is also not working –  Apr 23 '20 at 14:25

0 Answers0