multi-part file upload using RestAssured

Carvia Tech | December 16, 2019 | 3 min read | 3,618 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.

RestAssured gradle setup

We will add the latest rest-assured dependencies to our gradle build file.

dependencies {
    testImplementation ''
    testImplementation ''
    testImplementation ''
    testImplementation group: '', name: 'rest-assured', version: '4.1.2'

test {
	useJUnitPlatform()  (1)
1 Configuring JUNIT 5 Platform

You can check the latest version of rest-assured artifacts from

RestAssured File Upload

Now we will develop a RestAssured test that will upload a file and validate the status.

Lets assume that file upload controller has signature similar to deplicted by the below HTML form:

HTML form for file upload with multipart/form-data
<form id="uploadForm" action="/fileupload" method="post" enctype="multipart/form-data">
        <input type="file" name="file" size="40">
        <input type=submit value="Upload!">

The server response for successful file upload looks like this:

File upload response
  "success": true
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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);

    public void fileUploadTest() {
            .param("timestamp", new Date().getTime())
            .multiPart("file", new File("/path/to/file.json"))
            .body("success", equalTo(true));

RestAssured 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 ''

For maven users:


Advantages with Kotlin

Besides a more pleasing API for Kotlin developers it also has a couple of major benefits to the Java API:

  1. All failed expectations are reported at the same time

  2. Formatting the code in your IDE won’t mess up indentation

The same file upload testcase in Kotlin will lokk like below:

REST Assured based Fileupload testcase in Kotlin
fun `test rest assured file upload with kotlin`() {
    val success: Boolean =
        Given {
            multiPart("file", File("/path/to/file.json"))
        } When {
        } Then {
            body("success", equalTo(true))
        } Extract {
    println("operation status = $success")

That’s all.

You can download source for this article from Github repository:

Rest Assured:
  1. OAuth2 protected resources in RestAssured Testcases
  2. REST Assured Basic Authentication
  3. SDET: Rest Assured Interview Questions
See all articles in Rest Assured
Top articles in this category:
  1. SDET: Rest Assured Interview Questions
  2. SDET: JUnit interview questions for automation engineer
  3. Top 50 SDET Java Programming Interview Questions & Answers
  4. Top 15 coding problems for SDET Java Interviews
  5. OAuth2 protected resources in RestAssured Testcases
  6. Commonly used Http methods in RESTful services
  7. Get distinct words from a given file in Java

Find more on this topic:
SDET Interviews image
SDET Interviews

SDET Java Interview pattern and collection of questions covering SDET coding challenges, automation testing concepts, functional, api, integration, performance and security testing, junit5, testng, jmeter, selenium and rest assured

Last updated 1 week ago

Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info