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

Introduction

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.

Prerequisites

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 {
    jcenter()
}

dependencies {
	//REST Assured
    testImplementation 'io.rest-assured:rest-assured:4.2.0' 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="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	
	<groupId>com.roytuts</groupId>
	<artifactId>test-file-upload-download-rest-assured</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
                <maven.compiler.source>12</maven.compiler.source>
		<maven.compiler.target>12</maven.compiler.target>
	</properties>
	
	<dependencies>
		<dependency>
			<groupId>io.rest-assured</groupId>
			<artifactId>rest-assured</artifactId>
			<version>4.2.0 to 4.3.3</version>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>5.6.0 to 5.8.0-M1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.1</version>
			</plugin>
		</plugins>
	</build>
</project>

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

Testing

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

Download

Leave a Reply

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