File upload in RestAssured

Carvia Tech | July 10, 2019 | | 28 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. SDET: JUnit interview questions for automation engineer
  2. Top 50 SDET Java Programming Interview Questions & Answers
  3. OAuth2 protected resources in RestAssured Testcases
  4. Basic Authentication using RestAssured
  5. Commonly used Http methods in RESTful services
  6. SDET: Rest Assured Interview Questions
  7. Write a program to reverse the order of words in a string



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