API Testing Using RestAssured And Testkube

Jun 3, 2024
read
Bruno Lopes
Product Leader
Testkube
Read more from
Bruno Lopes

Table of Contents

Start your free trial.

Jun 3, 2024
read
Bruno Lopes
Product Leader
Testkube
Read more from
Bruno Lopes
Bruno Lopes
Product Leader
Testkube
This is an examination of the challenges of managing and executing API tests, and learn how to use RestAssured with Testkube.

Table of Contents

Executive Summary

Modern applications adopt API-first design, which provides seamless communication between services and clients. This approach is becoming increasingly popular, where APIs are designed and developed before the implementation of the actual services. This ensures that APIs are treated as first-class citizens, fostering consistency, reusability, and scalability.

Ensuring the reliability and availability of these APIs is crucial, as they are the backbone of critical business processes. API testing helps validate these services and ensure they work as expected and meet the set standards. 

In the Java ecosystem, RestAssured is a popular tool for API testing. However, managing and executing these tests is complicated as we move towards containerized applications. 

In this post, we’ll examine these challenges and learn how to use RestAssured with Testkube for API testing. 

RestAssured

RestAssured is a Java tool for testing APIs using Java libraries. It integrates well with build tools like Maven and Gradle, making it easy to work with. It enables automated testing of APIs by allowing developers to send simple HTTP requests and focus solely on testing the API without worrying about anything else. 

Below are some noteworthy features of RestAssured:

  • RestAssured integrates easily with other testing frameworks, such as JUnit and TestNG, making it easier to include API tests along with other automated tests. 
  • It supports all HTTP methods, enabling you to perform complete testing of CRUD-based functionalities.
  • It has out-of-the-box support for XML and JSON. This means you can use features like XMLPath and JSONPath to extract variables from responses. 
  • There is a robust set of assertion methods to validate the responses.

You can read more about RestAssured here.

Challenges using RestAssured in Kubernetes

RestAssured is a powerful tool for testing APIs in CI pipelines and local environments. However, running these tests in a Kubernetes environment has different challenges. 

  • Setting up environments for tests with required dependencies and configurations can be complex. 
  • Managing and scaling tests based on the load in the Kubernetes cluster can be challenging. 
  • Avoiding interference, maintaining consistency, and running tests in isolation is problematic. 
  • Integrating with CI/CD tools and maintaining test artifacts and logs can be a painful process.

Let us see how Testkube helps overcome these challenges. 

Using RestAssured With Testkube

Testkube is a Kubernetes-native testing framework that allows you to create testing workflows in a declarative, version-controlled way. It integrates with most CI/CD tools and helps you automate the initiation, execution, and validation of tests as part of automated workflows. 

Testkube allows you to plug in any testing tool and leverage the power of Kubernetes. It converts your tests, test suites, and other artifacts into Kubernetes CRDs, allowing you to manage them declaratively. 

With Testkube, you can create Test Workflows that include everything from provisioning necessary infrastructure components to integrating seamlessly with other testing tools and orchestrating complex tests. Refer to our Test Workflows documentation to learn more. 

Let's examine how to use RestAssured with Testkube for API testing. We’ll create a Test Workflow using Gradle and integrate RestAssured tests into it. This repo contains all the required files for this example.

Prerequisites

Once the prerequisites are in place, you should have a target Kubernetes cluster ready with a Testkube agent configured. 

Creating a Test Workflow

Navigate to the Test Workflows tab and click “Add a new test workflow.”

This will provide you with three options:

  • Create from scratch - use the wizard to create a Test Workflow.
  • Start from an example - use existing k6, Cypress, and Playwright examples.
  • Import from yaml - import your own Test Workflow.

We’ll choose the “create from scratch” option to create this workflow.

  • Provide a name for the workflow and choose the type as Gradle. 
  • Provide the run command. In this case, we’ll provide gradlew test
  • Provide a Gradle version. We’ll use 8.5.0-jdk11

On the next screen, provide the source for the test file. This can either be a Git repo, a string, or a file. In this case, we’ll use a Git repo. 

On the next screen, it will generate the YAML spec file and display the output. 

The YAML file is self-explanatory, as it lists the details you’ve provided. We have added extra parameters for artifacts that will collect the reports generated by RestAssured and store them. 

The RestAssured test file in the repo defines what we are testing.

The test checks for the response from https://jsonplaceholder.typicode.com endpoint and validates the response.

The repo contains other files, including test steps and a test runner, which contain related code for executing the RestAssured test using Gradle.

Click on “Create” to create the test workflow.

Executing the Test Workflow

Once the workflow is ready, you’ll see the newly created test workflow on the screen. Click on it and click “Run Now” to start the workflow.

You’ll see the workflow executing along with the real-time logs of every step. 

You’ll see the test result based on the test execution. In this case, you’ll see the test pass.

Since we have configured the artifacts for this, you can navigate to the Artifacts tab and look at the reports generated by RestAssured. Testkube saves these reports for every execution, making it easier to analyze the tests.

This was a simple demo of creating a RestAssured Test Workflow using Gradle for Kubernetes testing. To get more out of Test Workflows, you can create custom workflows and import them to Testkube.

Summary

In this post, we looked at how the API-first design approach is getting popular. Many Java applications are also adopting this design principle, and tools like RestAssured are gaining popularity in testing APIs. We also looked at RestAssured and the complexities of running it on Kubernetes. 

We then saw how, using RestAssured with Testkube, you can create an end-to-end workflow for API testing, leveraging the power of Kubernetes.

Visit the Testkube website to learn more about the other testing tools you can integrate with. If you struggle with anything, feel free to post a note in our active Slack community.

Frequently Asked Questions

What is RestAssured used for?

RestAssured is a Java library for testing REST APIs. You send HTTP requests in code, then validate status codes, headers, and JSON or XML response bodies with readable assertions. It works with JUnit and TestNG and builds with Maven or Gradle.

Can I run RestAssured tests in Kubernetes?

Yes. With Testkube, you create a Test Workflow that pulls your RestAssured project from Git and runs it with Gradle inside your cluster. Testkube handles execution, logs, and artifacts, so you don't have to build custom jobs or runners.

How do I run a RestAssured test with Testkube?

Configure the Testkube Agent on your cluster, then add a new Test Workflow from scratch, from an example, or from YAML. Choose Gradle as the type, set the run command (for example, gradlew test), point the workflow at your Git repo, and click Run Now.

Where can I find RestAssured test reports in Testkube?

Add an artifacts section to your Test Workflow, and Testkube collects the reports RestAssured generates and stores them for every execution. Open an execution and go to the Artifacts tab to view them.

Does Testkube work with my existing CI/CD pipeline?

Yes. Testkube integrates with most CI/CD tools, so you can trigger your RestAssured workflows from your pipeline while keeping logs, results, and artifacts in one place.

{{banner-get-started}}

Bruno Lopes
Product Leader
Testkube
Read more from
Bruno Lopes

About Testkube

Testkube is the open testing platform for AI-driven engineering teams. It runs tests directly in your Kubernetes clusters, works with any CI/CD system, and supports every testing tool your team uses. By removing CI/CD bottlenecks, Testkube helps teams ship faster with confidence.
Get Started with a trial to see Testkube in action.