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


In this tutorial we will 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. We are going to use here Rest Assured library with Junit 5 framework in Java programming language.

We 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.


Eclipse 2019-12, Java at least 8, Rest Assured Library, Junit 5


  • You should place rest-assured 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 ''

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

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

<project xmlns="" xmlns:xsi=""
		<java.version>at least 1.8</java.version>

File Upload Test

We use http POST method to upload the file. The server URL sends a response with original file name. So we are 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 we will test our file download. We will download a file from server and verify the content type of the file.

Here we are 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 will give you the expected output.

Related Posts:

That’s all.


Thanks for reading.

Leave a Comment