File upload in RestAssured

Carvia Tech | July 10, 2019 | | 6 views


In this article we will learn how to do file upload to server endpoint using RestAssured library.

Let’s assume you have a controller that accepts a param and a file and returns a json with status. You can write a RestAssured test that will upload a file and check the status, as shown in below code snippet.

The server response looks like this:

File upload response
{
  "status": true
}
FileUploadTest.java
import io.restassured.http.ContentType;
import org.junit.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("param", "foo")
                .multiPart(new File("/path/to/file.json"))
                .accept(ContentType.JSON)
                .expect()
                .body("status", equalTo(true))
                .when()
                .post("http://localhost:8080/upload");
    }
}

Top articles in this category:
  1. Top 50 SDET Java Programming Interview Questions & Answers
  2. OAuth2 protected resources in Rest Assured Testcases
  3. Basic Authentication using RestAssured
  4. Commonly used Http methods in RESTful services
  5. Write a program to reverse the order of words in a string
  6. Java program to check if two strings are anagrams
  7. Write a program to reverse a string using recursion in Java



Find more on this topic:
SDET Interviews image
SDET Interviews

End to end automation testing using Selenium Web Driver, Rest Assured, JMeter, Junit, TestNG etc.

Last updated 1 month ago


Recommended books for interview preparation:

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