'Spring Boot: Write Integrationtest for passing json data alongside multiple files

i am trying to write a controller for signing up users in Spring Boot with Kotlin. Here is my code:

@PostMapping
fun signup(@RequestParam(name = "signupRequest") signupRequest: SignupRequest, 
           @RequestPart profileImage: MultipartFile?,
           @RequestPart additionalFiles: List<MultipartFile>?): ResponseEntity<UserResponse> { 
    ...
}

This is working fine so far when testing it with Postman. My question is how to properly write and Integration test for this?

This is my latest approach:

    @Test
    fun signupSuccessfulTest() {
        val profileImage = MockMultipartFile("${signupRequest.firstName}_${signupRequest.lastName}${Constants.IMAGE_FILE_EXTENSION}",
            FileInputStream("./${Constants.TEST_FILES_DIRECTORY}profile_image/profile_image.png"))

        val result: MvcResult = mockMvc.perform(
            MockMvcRequestBuilders.multipart(FULL_API_PATH)
                .file(profileImage)
                .param("signupRequest", mapper.writeValueAsString(signupRequest)))
            .andExpect(status().isCreated).andReturn()

}

I tried several different ways now and I just can't get it to work.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source