Create a JUnit 5 Test Case with Rest Assured. Video Tutorial.

In this JUnit 5 and Rest Assured tutorial, I am going to share with you how to add JUnit 5 and Rest Assured support to your Spring Boot project. It will be more than that actually. This tutorial will cover:

  • Add JUnit 5 and Rest Assured to Spring Boot Project,
  • Create a new JUnit 5 Test Case,
  • Create a new Rest Assured Test method to test Create User API Call.

If you watch video tutorials included in this blog post, you will learn how to use Rest Assured to:

  • Create a new HTTP Post Request,
  • How to set HTTP Request Body,
  • How to read JSON from HTTP Response Body,
  • And how to validate selected key-value pairs from JSON payload read from HTTP Response.

There are many more videos you can watch and learn how to:

  • Build RESTful Web Services with Spring Boot,
  • How to Deploy your RESTful Web Service to Amazon cloud,
  • and how to work with Rest Assured to test your RESTful Web Service

You can find all these videos on my page here: RESTful Web Services with Spring Boot, Spring MVC, JPA and MySQL

I hope this short tutorial will be of some value to you.

Add JUnit 5 Dependencies to Spring Boot Project

Let’s begin with adding JUnit 5 to our Spring Boot project. Open your project’s pom.xml file and add the following dependencies:

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
 <groupId>org.junit.jupiter</groupId>
 <artifactId>junit-jupiter-engine</artifactId>
 <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
<dependency>
 <groupId>org.junit.platform</groupId>
 <artifactId>junit-platform-launcher</artifactId>
 <version>1.2.0</version>
 <scope>test</scope>
</dependency>

Add Rest Assured Dependencies to Spring Boot Project

After you have added the JUnit 5 Dependencies, let’s add to our pom.xml file the following dependencies needed for Rest Assured.

  <dependency>
   <groupId>io.rest-assured</groupId>
   <artifactId>rest-assured</artifactId>
   <scope>test</scope>
  </dependency>

  <dependency>
   <groupId>io.rest-assured</groupId>
   <artifactId>json-path</artifactId>
  </dependency>

  <dependency>
   <groupId>io.rest-assured</groupId>
   <artifactId>json-schema-validator</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>io.rest-assured</groupId>
      <artifactId>xml-path</artifactId>
      <version>3.1.1</version>
  </dependency>

Remove Hamcrest from Spring Boot Starter

There is a little incompatibility with Hamcrest libraries and if we run our project as is now we will get an error message. To resolve an issue with Hamcrest libraries, we will need to remove the hamcrest-core from Spring Boot Starter like in the example below. You will still have the hamcrest-core libraries in your project. It is just they will be not brought in with the Spring Boot Starter bundle.

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter</artifactId>
 <exclusions>
  <exclusion>  <!-- declare the exclusion here -->
   <groupId>org.hamcrest</groupId>
   <artifactId>hamcrest-core</artifactId>
  </exclusion>
 </exclusions>
</dependency>

Add the maven-surefire-plugin to your POM.xml

For you to be able to build your project and run JUnit 5 test cases with maven command you will need to update the <build> block in your pom xml file by adding the maven-surefire-plugin. Below is my complete <build> element example which works well:

<build>
 <plugins>
  <plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
  </plugin>
  <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <dependencies>
    <dependency>
     <groupId>org.junit.platform</groupId>
     <artifactId>junit-platform-surefire-provider</artifactId>
     <version>1.2.0</version>
    </dependency>
   </dependencies>
  </plugin>
 </plugins>
</build>

Complete POM.XML file

And here is the complete POM.xml file of my Spring Boot Project that has JUnit 5 and Rest Assured added and working.

<?xml version="1.0" encoding="UTF-8"?>
<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.appsdeveloperblog.app.ws</groupId>
 <artifactId>mobile-app-ws-rest-assured-test</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>mobile-app-ws-rest-assured-test</name>
 <description>Demo project for Spring Boot</description>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.4.RELEASE</version>
  <relativePath /> <!-- lookup parent from repository -->
 </parent>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter</artifactId>
   <exclusions>
    <exclusion>  <!-- declare the exclusion here -->
     <groupId>org.hamcrest</groupId>
     <artifactId>hamcrest-core</artifactId>
    </exclusion>
   </exclusions>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>

  <dependency>
   <groupId>io.rest-assured</groupId>
   <artifactId>rest-assured</artifactId>
   <scope>test</scope>
  </dependency>

  <dependency>
   <groupId>io.rest-assured</groupId>
   <artifactId>json-path</artifactId>
  </dependency>

  <dependency>
   <groupId>io.rest-assured</groupId>
   <artifactId>json-schema-validator</artifactId>
   <scope>test</scope>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
  <dependency>
   <groupId>org.junit.jupiter</groupId>
   <artifactId>junit-jupiter-engine</artifactId>
   <scope>test</scope>
  </dependency>

  <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
  <dependency>
   <groupId>org.junit.platform</groupId>
   <artifactId>junit-platform-launcher</artifactId>
   <version>1.2.0</version>
   <scope>test</scope>
  </dependency>

 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <dependencies>
     <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-surefire-provider</artifactId>
      <version>1.2.0</version>
     </dependency>
    </dependencies>
   </plugin>
  </plugins>
 </build>
</project>

Conclusion

In conclusion, creating a JUnit 5 test case with Rest Assured allows developers to perform comprehensive and automated testing of RESTful APIs within a Spring Boot project. The video tutorials below cover the necessary steps to set up and execute JUnit 5 test cases using Rest Assured.

Ready to become proficient in testing RESTful web services? Visit the Testing Java Code page and explore our comprehensive tutorial on leveraging REST Assured. Learn how to write concise and robust tests, handle different HTTP methods, and validate API responses effectively.

Video Demonstration

Below are video tutorials that demonstrate how to do it.

I hope this was helpful!

If you are interested in learning more about building, testing, and deploying RESTful Web Services you can check this page:  RESTful Web Services with Spring Boot, Spring MVC, JPA, and MySQL or you can check the list of video courses below I hope one of them will be just what you are looking for.

Happy learning!