dependencies {
testImplementation 'io.rest-assured:json-path:4.1.2'
testImplementation 'io.rest-assured:xml-path:4.1.2'
testImplementation 'io.rest-assured:kotlin-extensions:4.1.2'
testImplementation group: 'io.rest-assured', name: 'rest-assured', version: '4.1.2'
}
test {
useJUnitPlatform() (1)
}
RestAssured multipart file upload
Carvia Tech | September 12, 2020 | 3 min read | 5,811 views | Rest Assured
In this article we will learn how to test multipart/form-data file upload feature using RestAssured library, various assertions, and kotlin support.
Let’s assume you have a fileupload server that accepts a param + a file and returns a json with upload status. You can download such Spring Boot based server from this github repository:
Optionally, you can follow Spring Boot file upload with Gradle, Kotlin and Junit 5 to setup the file upload server.
REST Assured gradle setup
We will add the latest rest-assured dependencies to our gradle build file.
1 | Configuring JUNIT 5 Platform |
You can check the latest version of rest-assured
artifacts from https://mvnrepository.com/artifact/io.rest-assured/rest-assured
REST Assured File Upload
Now we will develop a RestAssured test that uploads a file and validate the status.
Let’s assume that file upload controller has signature similar to depicted by the below HTML form:
<form id="uploadForm" action="/fileupload" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="40">
<input type=submit value="Upload!">
</form>
The server response for successful file upload looks like this:
{
"success": true
}
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.Date;
import static io.restassured.RestAssured.given;
import static org.hamcrest.core.IsEqual.equalTo;
public class FileUploadTest {
private final Logger logger = LoggerFactory.getLogger(FileUploadTest.class);
@Test
public void fileUploadTest() {
given()
.param("timestamp", new Date().getTime())
.multiPart("file", new File("/path/to/file.json"))
.accept(ContentType.JSON)
.when()
.post("http://localhost:8080/fileupload")
.then()
.statusCode(200)
.body("success", equalTo(true));
}
}
REST Assured FileUpload Kotlin version
REST Assured has introduced a new module called "kotlin-extensions" which provides useful extension functions when working with Kotlin.
You can add this kotlin extension library to your project to start developing beautiful rest assured testcases written in kotlin.
testImplementation 'io.rest-assured:kotlin-extensions:4.1.2'
For maven users:
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>kotlin-extensions</artifactId>
<version>4.1.2</version>
<scope>test</scope>
</dependency>
Advantages with Kotlin
Besides a more pleasing API for Kotlin developers it also has a couple of major benefits to the Java API:
-
All failed expectations are reported at the same time
-
Formatting the code in your IDE won’t mess up indentation
The same file upload testcase in Kotlin will look like below:
@Test
fun `test rest assured file upload with kotlin`() {
val success: Boolean =
Given {
port(8080)
multiPart("file", File("/path/to/file.json"))
accept(ContentType.JSON)
} When {
post("http://localhost:8080/fileupload")
} Then {
statusCode(200)
body("success", equalTo(true))
} Extract {
path("success")
}
println("operation status = $success")
}
That’s all.
You can download source for this article from Github repository:
Rest Assured:
- Rest Assured API Testing Interview Questions
- REST Assured Basic Authentication
- OAuth2 protected resources in RestAssured Testcases
Top articles in this category:
- Rest Assured API Testing Interview Questions
- SDET: JUnit interview questions for automation engineer
- 50 SDET Java Interview Questions & Answers
- OAuth2 protected resources in RestAssured Testcases
- SDET Java Coding Challenges
- h2load for REST API benchmarking
- Get distinct words from a given file in Java
Find more on this topic:
Subscribe to Interview Questions
Recommended books for interview preparation:
Similar Posts
- Reverse position of words in a string using recursion
- REST Assured with plain/text response body
- Get distinct words from a given file in Java
- SDET Java Coding Challenges
- REST Assured vs Apache HttpClient and RestTemplate
- Java 11 HttpClient with Basic Authentication
- HTTP GET request with Java 11 HttpClient - Kotlin
- HTTP Head request using Java 11 HttpClient - Kotlin
- Using Java 11 HttpClient with Kotlin Coroutines
- Migrating Spring Boot tests from Junit 4 to Junit 5