0
votes

I'm building rest-assured test for rest controller. Rest-assured test:

@Test
fun saveFileReturnsFileKeyAndStatusCreated() {
    given()
    .multiPart("file", File("d:/2.txt"))
    .multiPart("fileDescription", "...file description here...")
    .multiPart("fileExtension", ".txt")
    .`when`()
    .post("/file")
    .then()
    .statusCode(HttpStatus.CREATED.value())
    .body(notNullValue<String>(String::class.java))
}

Rest controller method:

@RestController
@RequestMapping(produces = arrayOf(MediaType.APPLICATION_JSON_UTF8_VALUE))
class ClientActionsController(private var clientActionsService: ClientActionsService) {

    @PostMapping(value = "/file", consumes = arrayOf(MediaType.MULTIPART_FORM_DATA_VALUE))
    fun saveFile(request: HttpServletRequest): ResponseEntity<String> {
        println(request.getPart("fileDescription"))
        println(request.getPart("fileExtension"))
        println(request.getPart("file"))

        return ResponseEntity(clientActionsService.saveFile(request), HttpStatus.CREATED)
    }
}

Real code works fine, but when I start the test all the parts in request are null. What might be the cause for not receiving rest-assured's multiparts in rest controller's HttpServletRequest?

Spring Boot 1.5.8, rest-assured 3.0.5

1
Are you using the RestAssuredMockMvc API or standard RestAssured? - Johan

1 Answers

0
votes

The reason your file is null, because in your test configuration class, you need to have @bean for MultipartFileResolver.

Something like this:

@Bean public MultipartResolver multipartResolver() { return new CommonsMultipartResolver(); }