Download SpringBoot image using HTTP message and test with TestNG and MockMvc

I have a calm web service that accepts uploading images using an HTTP message. I checked the test with TestNG and MockMvc to check if the image was loaded correctly in the database. This is my download service:

@RequestMapping(value = "/image", method = RequestMethod.POST,consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},produces = { MediaType.APPLICATION_JSON_VALUE}) public ResponseDTO uploadImage(@RequestParam(value = "file", required = true) MultipartFile file) throws IOException { if(file == null) return prepareResponse(null,"There are problems to upload the current file",HttpStatus.BAD_REQUEST); MediaImage mediaImage = MediaImage.builder().imageByte(file.getBytes()).contentType(file.getContentType()).originalName(file.getOriginalFilename()).created(new Date()).build(); return prepareResponse(mediaImageCrudControllerImpl.create(mediaImage),"Image correctly uploaded",HttpStatus.OK); } 

This is my TestNg test:

 @WebIntegrationTest @SpringApplicationConfiguration(classes = { backend.ContextConfiguration.class }) public class MediaImageTestImplIT extends AbstractTestNGSpringContextTests{ @Autowired protected WebApplicationContext webApplicationContext; @NotNull protected MockMvc mockMvc; @BeforeMethod protected void setup() throws Exception { this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void createMediaImage() throws IOException, Exception{ File image =new ClassPathResource("/img/Desert.jpg").getFile(); if(image!=null){ FileInputStream f = new FileInputStream(image); MockMultipartFile secmp = new MockMultipartFile("file", "Desert.jpg","multipart/form-data",f); MvcResult result = mockMvc. perform(MockMvcRequestBuilders.fileUpload(Constants.MEDIA_IMAGE_ENDPOINT). file(secmp)). andExpect(status().isOk()). andReturn(); } } 

}

The code is working fine, but it took more than 200 seconds, and I don't understand why. If I use the rest service with a client application, it is very fast. What could be the problem with the test?

0
spring-boot testng file-upload multipart mockmvc
Jul 06 '17 at 8:12
source share

No one has answered this question yet.

See related questions:

1449
Preview image before uploading it.
377
Using curl to load POST data with files
3
Unit test Springboot MockMvc returns 403 Forbidden
2
POST MockMvc Request
one
Unit testing for Singleton through Enum raises java.lang.NoClassDefFoundError
one
Spring Boot + TestNG + MockMVC gives Null for @Autowired



All Articles