File Upload and File Download REST APIs Testing using Rest Assured in Java


In this tutorial I will show you how to test REST APIs used for file upload and file download. The file upload or download application might have been created using any server side technology. I am going to use here Rest Assured library with Junit 5 framework in Java programming language.

I will use multipart for file upload test. For download test you don’t need such content type – multipart. The fluent API used by REST Assured supports the familiar¬†Given/When/Then¬†syntax from behavior driven development (BDD) framework, resulting in a test that is easy to read and takes care of setup, execution, and verification with just a single line of code.


Java at least 8, Rest Assured Library 4.2.0 – 4.3.3, Junit 5.6.0 – 5.8.0-M1

Important Note:

  • You should place rest-assured dependency before the JUnit dependency declaration in your pom.xml / build.gradle in order to make sure that the correct version of Hamcrest is used.
  • REST Assured includes JsonPath and XmlPath as transitive dependencies

Posts you need to go through first:

Build Script or File

If you are using gradle based build tool then you can use below build.gradle script:

plugins {
    id 'java-library'

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {
	//REST Assured
    testImplementation '' to 4.3.3

    // Use JUnit test framework
    testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.6.0' to 5.8.0-M1

If you are using maven based project then you can use pom.xml file:

<project xmlns="" xmlns:xsi=""
			<version>4.2.0 to 4.3.3</version>
			<version>5.6.0 to 5.8.0-M1</version>

File Upload Test

I use http POST method to upload the file. The server URL sends a response with original file name. So I am comparing expected and actual result to ensure our test case works fine.

String resp = RestAssured.given().multiPart("file", new File("src/test/resources/info.xlsx")).when().post("http://localhost:8080/upload").then().assertThat().statusCode(200).and().extract().body().asString();

assertEquals(resp, "info.xlsx");

File Download Test

Now I will test our file download. I will download a file from server and verify the content type of the file.

Here I am using http GET method to download the file.

String contentType = RestAssured.given().when().get("http://localhost:8080/download").then().assertThat().statusCode(200).and().extract().contentType();

assertEquals(contentType, "text/json");


Now running the above two test cases as Junit tests and will give you the expected output.

Related Posts:

That’s all about how to test file upload and download tests.

Source Code


Leave a Reply

Your email address will not be published. Required fields are marked *