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

Introduction

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.

Prerequisites

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

Note:

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

dependencies {
	//REST Assured
    testImplementation 'io.rest-assured:rest-assured:4.2.0'

    // 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="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>
		<java.version>at least 1.8</java.version>
	</properties>
	
	<dependencies>
		<dependency>
			<groupId>io.rest-assured</groupId>
			<artifactId>rest-assured</artifactId>
			<version>4.2.0</version>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-engine</artifactId>
			<version>5.6.0</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>
                <configuration>
					<source>${java.version}</source>
					<target>${java.version}</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

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

Testing

Now running the above two test cases will give you the expected output.

Related Posts:

That’s all.

Download

Thanks for reading.

Related posts

Leave a Comment