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 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:
You can read more about RestAssured here.
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.
Let us see how Testkube helps overcome these challenges.
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.
Once the prerequisites are in place, you should have a target Kubernetes cluster ready with a Testkube agent configured.
Navigate to the Test Workflows tab and click on “Add a new test workflow”
This will provide you with three options:
We’ll choose the “create from scratch” option to create this workflow.
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.
```yaml
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.
```java
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.
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.
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.
Testkube is a test execution and orchestration framework for Kubernetes that works with any CI/CD system and testing tool you need, empowering teams to deliver on the promise of agile, efficient, and comprehensive testing programs by leveraging all the capabilities of K8s to eliminate CI/CD bottlenecks, perfecting your testing workflow. Get started with Testkube's free trial today!
Related topics: