I have following POST endpoint inside my Quarkus project:
@PUT
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("{var}/some-def")
public Response saveReportDefinition(
@PathParam("var")
String var,
@NotNull
@Valid
FileUploadInput fileInput,
@Context
UriInfo uriInfo
) {
return Response.ok().build();
}
where my FileUploadInput looks lie this:
import org.jboss.resteasy.reactive.multipart.FileUpload;
public record FileUploadInput(
@NotNull
@RestForm("file1")
FileUpload file1,
@NotNull
@RestForm("file2")
FileUpload file2,
@NotNull
@RestForm("file3")
FileUpload file3
) {}
I have following test method with RestAssured framework:
@Test
@DisplayName("Test Upload")
public void testUpload() {
// 1. Prepare test data
File file1 = getResourceAsFile("/dir/file1.json");
File file2 = getResourceAsFile("/dir/file2.txt");
File file3 = getResourceAsFile("/dir/file3.sql");
// 2. Construct the request
Response response = given()
.contentType(ContentType.MULTIPART)
.multiPart(?????)
.put(BASE_URL + "Commission/report-definition");
// 3. Validate the response
response.then()
.statusCode(200) // Expecting a successful response (HTTP 200)
.body(is(emptyOrNullString())); // Assuming empty or null response body
}
Now how to write proper test with restassured framework for such endpoint?
I have written following test according to @geoand (thanks for answer) instructions which failed
@Test
@DisplayName("Test Upload")
public void testUpload() {
// 1. Prepare test data
File file1 = getResourceAsFile("/dir/file1.json");
File file2 = getResourceAsFile("/dir/file2.txt");
File file3 = getResourceAsFile("/dir/file3.sql");
// 2. Construct the request
Response response = given()
.contentType(ContentType.MULTIPART)
.multiPart("metaFile", file1, MediaType.TEXT_PLAIN)
.multiPart("jsonFile", file2, MediaType.APPLICATION_JSON)
.multiPart("sqlFile", file3, MediaType.TEXT_PLAIN)
.put(BASE_URL + "Commission/report-definition");
// 3. Validate the response
response.then()
.statusCode(200) // Expecting a successful response (HTTP 200)
.body(is(emptyOrNullString())); // Assuming empty or null response body
}
but if I implemented saveReportDefinition()
without record like
@PUT
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("{var}/some-def")
public Response saveReportDefinition(
@PathParam("var")
String var,
@NotNull
@RestForm("file1")
FileUpload file1,
@NotNull
@RestForm("file2")
FileUpload file2,
@NotNull
@RestForm("file3")
FileUpload file3
@Context
UriInfo uriInfo
) {
return Response.ok().build();
}
then everything worked. Then I tried to model FileUploadInput
as class and not like a record as before
import org.jboss.resteasy.reactive.multipart.FileUpload;
public class FileUploadInput {
@NotNull
@RestForm("file1")
public FileUpload file1,
@NotNull
@RestForm("file2")
public FileUpload file2,
@NotNull
@RestForm("file3")
public FileUpload file3
}
and then automagically test started working. I do not know why does the records do not work in testing. I am using Quarkus v. 3.8.4 with Java 17 Temurin.