1

I want to send to the controller a complex object consisting of files and simple types.

public class ContributionNew<T extends MovieInfoDTO> {
    private List<T> elementsToAdd;
    private Map<Long, T> elementsToUpdate;
    private Set<Long> idsToDelete;
    private Set<String> sources;
    private String comment;
}

public class Photo extends MovieInfoDTO {
    private MultipartFile photo;
}

@PostMapping(value = "/{id}/contributions/photos")
@ResponseStatus(HttpStatus.CREATED)
public
ResponseEntity<Void> createPhotoContribution(
        @ApiParam(value = "The movie ID", required = true)
        @PathVariable("id") final Long id,
        @ApiParam(value = "The contribution", required = true)
        @RequestBody @Valid final ContributionNew<Photo> contribution
) {

I want to create a test to send an object, but I do not know how to finish it.

@Test
public void testCreatePhotoContribution() throws Exception {
    ContributionNew<Photo> contribution = new ContributionNew<>();
    MockMultipartFile multipartFile = new MockMultipartFile("photo", "C:\\Users\\Jonatan\\Pictures\\2.png",
            "image/png", "Spring Framework".getBytes());
    Photo.Builder photoBuilder = new Photo.Builder(
            multipartFile
    );
    contribution.getElementsToAdd().add(photoBuilder.build());

    mockMvc
            .perform(post("/api/v1.0/movies/{id}/contributions/photos", 1)
                .contentType(...)
                .content(...))
            .andExpect(status().isCreated());
}

I do not know how to send such an object as @ResuestBody? I do not know how to finish this test.

sfsdfgdsg
  • 11
  • 1

1 Answers1

0

You can do something like this.

 ObjectMapper = new ObjectMapper();        // You can also Autowire this 
 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    mockMvc
     .perform(post("/api/v1.0/movies/{id}/contributions/photos", 1)
     .contentType(MediaType.APPLICATION_JSON)                   
     .content(objectMapper.writeValueAsString(contribution)))
     .andExpect(status().isCreated());
pvpkiran
  • 25,582
  • 8
  • 87
  • 134