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.
Considering the Java ecosystem, one popular tool for testing APIs is RestAssured. Tailored for the Java ecosystem, RestAssure 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 automation 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 of the 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 RestAssure tests into it. This repo contains all the required files for this example.
Pre-requisites
- Get a Testkube account.
- Kubernetes cluster - we're using a local Minikube cluster.
- Testkube Agent configured on the cluster.
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 on "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, 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.
kind: TestWorkflow
apiVersion: testworkflows.testkube.io/v1
metadata:
name: gradle-restassured
namespace: testkube
labels:
test-workflow-templates: "yes"
spec:
use:
- name: official--gradle--beta
config:
run: ./gradlew test
version: 8.5.0-jdk11
content:
git:
uri: https://github.com/kubeshop/testkube-examples.git
revision: main
paths:
- RestAssured Test Using Gradle
container:
workingDir: /data/repo/RestAssured Test Using Gradle
steps:
- artifacts:
workingDir: /data/repo/RestAssured Test Using Gradle/app/build/
paths:
- '**/*'
The yaml file is self-explanatory as it lists down the details you've provided in the yaml. We have added extra parameters for artifacts that will collect the reports generated by RestAssured and store them.
Below is the RestAssured test file that explains what we are testing.
package org.example;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
public class ApiTest {
@Test
public void testGetEndpoint() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
given().
when().
get("/posts/1").
then().
statusCode(200).
body("userId", equalTo(1)).
body("id", equalTo(1)).
body("title", not(empty())).
body("body", not(empty()));
}
}
The test checks for the response from https://jsonplaceholder.typicode.com endpoint and validates if 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 take advantage of test workflows more, you can create custom workflows and import them to Testkube.
Summary
In this post we looked at how 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.