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?
spring-boot testng file-upload multipart mockmvc
Paolo Jul 06 '17 at 8:12 2017-07-06 08:12
source share