Simplify BDD Testing for Distributed Java Applications with Testkube

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

Table of Contents

Start your free trial.

Start your free trial.

Start your free trial.

Explore Testkube hands-on.
30 days
no commitment
$0
no credit card needed

Subscribe to our monthly newsletter to stay up to date with all-things Testkube.

Please disable pixel blocker extension
You have successfully subscribed to the Testkube newsletter.
You have successfully subscribed to the Testkube newsletter.
Oops! Something went wrong while submitting the form.
Jun 3, 2024
read
Bruno Lopes
Product Leader
Testkube
Read more from
Bruno Lopes
Bruno Lopes
Product Leader
Testkube
This is an exploration of how Testkube can streamline BDD testing using Cucumber for distributed Java applications, making the process more efficient and manageable.

Table of Contents

Executive Summary

As the trend towards distributed applications continues, testing Java applications has become increasingly complex. Distributed systems involve numerous microservices, making it essential to ensure consistency, manage failures, and verify data integrity across all components. Comprehensive end-to-end testing is crucial to guarantee the application behaves as expected.

Behavior-Driven Development (BDD) addresses these challenges by defining application behavior in natural language, simplifying the explanation and understanding of test cases for distributed applications.

However, integrating BDD into testing distributed Java applications presents its own set of challenges. Maintaining BDD scenarios for multiple services, orchestrating testing environments, and managing services and test data can be daunting tasks.

In this blog post, we will explore how Testkube can streamline BDD testing using Cucumber for distributed Java applications, making the process more efficient and manageable.

Cucumber - BDD Testing Tool for Java

Cucumber is one of the most widely used BDD testing tools. It uses Gherkin, which allows teams to define application features using natural language. It includes support for different programming languages, including Java. It also integrates well with popular development frameworks, making it a popular choice. Lastly, it generates detailed reports, giving you insights into testing. 

While Cucumber is a great tool for implementing BDD, it has challenges when testing distributed applications, especially in a Kubernetes environment.

  • Handling test data can be daunting, especially in a distributed environment like Kubernetes. Cucumber requires sharing state and test data between steps, thus ensuring isolation is complex yet critical.
  • Resources such as compute, memory, and storage are dynamically provisioned in Kubernetes. Cucumber needs access to these resources, and configuring this is difficult.
  • Aggregating test results from distributed Cucumber test runs is challenging and may require additional setup to bring all of them under one dashboard.

While Kubernetes automates deployments, scaling, and app management, it adds an additional layer of abstraction that needs to be managed along with configuring Cucumber to work with it. This is where a tool like Testkube comes into the picture.

Using Cucumber with Testkube for BDD Testing

Testkube provides a unified testing framework built for Kubernetes to simplify and streamline the entire testing process. The test team can store, execute, and manage tests on Kubernetes clusters. It defines tests as Kubernetes CRDs to provide a modern solution for managing all your tests and scaling when needed.

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 Cucumber with Testkube for BDD testing. We’ll create a Test Workflow using Gradle and integrate Cucumber 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 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 `gradle test`. 
  • Provide a Gradle version. We’ll use `8.5.0-jdk11`.
Testkube Create a Test Workflow wizard with the Gradle type, run command and Gradle version for the Cucumber test

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. 

Selecting a Git repository as the test source in the Testkube Test Workflow wizard

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

Generated Test Workflow YAML spec for the Gradle Cucumber test in Testkube

We will update the YAML by adding `condition: always` to the artifacts step to ensure that logs are always collected, regardless of whether any step failed.

The YAML file is self-explanatory, as it lists the details you’ve provided in the wizard. The repo also includes the Cucumber feature file that describes the behavior under test.

It also contains the step definitions and a test runner with the code that executes the Cucumber test.

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. 

Cucumber Test Workflow running in Testkube with real-time logs for each step

You’ll see the test result based on the test execution. In this case, we modified the test to fail, so you’ll see the failure. 

Failed Cucumber test in the Testkube execution logs for the gradle-cucumber workflow

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

Cucumber test reports in the Testkube Artifacts tab for a Test Workflow execution
Gradle Cucumber test report showing a failed scenario, opened from Testkube artifacts

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

Summary

This post summarizes the intricacies of running BDD tests for testing distributed Java applications. We looked at Cucumber, a popular BDD testing tool, and the complexities of running it on Kubernetes. 

We also looked at Test Workflows, which bring the entire testing workflow into a single file where you can manage all aspects of your testing. The demo showed how to use Cucumber for BDD testing of Java applications. 

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 BDD testing with Cucumber?

Cucumber is a behavior-driven development (BDD) tool that lets teams write test scenarios in Gherkin, a plain-language Given/When/Then syntax. Step definitions written in Java (or another supported language) turn each step into executable code, so product and engineering teams share one definition of how the application should behave.

Why is BDD testing harder for distributed Java applications?

A single scenario often spans several microservices, so tests need shared test data, isolated state and access to resources that Kubernetes provisions dynamically. Results from separate test runs also have to be collected in one place before you can tell whether a feature works end to end.

How does Testkube run Cucumber tests on Kubernetes?

Testkube runs Cucumber tests as Test Workflows inside your cluster. In this example, the workflow uses the Gradle type, pulls the project from a Git repo and runs gradle test with a Gradle 8.5.0 JDK 11 image, while Testkube streams the logs for every step.

How do I keep Cucumber reports when a test fails?

Add condition: always to the artifacts step of your Test Workflow. Testkube then collects the logs and Cucumber reports for every execution, including failed runs, and you can open them from the execution’s Artifacts tab.

Can I run Cucumber tests with Maven instead of Gradle?

Yes. Test Workflows also support Maven projects. You would use a Maven image and run mvn test instead of gradle test, and the rest of the workflow, including the Git source and artifact collection, works the same way.

{{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.