Responsive

Simplify BDD Testing for Distributed Java Applications with Testkube

Simplify BDD Testing for Distributed Java Applications with Testkube

Last updated
August 12, 2024
Bruno Lopes
Product Leader
Testkube
Share on X
Share on LinkedIn
Share on Reddit
Share on HackerNews
Copy URL

Table of Contents

Start Using Testkube with a Free Trial Today!

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

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 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 are 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 know 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.

Pre-requisites

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`

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. 

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

```yaml

kind: TestWorkflow

apiVersion: testworkflows.testkube.io/v1

metadata:

  name: gradle-cucumber

  namespace: testkube

  labels:

    test-workflow-templates: "yes"

spec:

  use:

  - name: official--gradle--beta

    config:

      run: gradle test

      version: 8.5.0-jdk11

  content:

    git:

      uri: https://github.com/kubeshop/testkube-examples.git

      revision: main

      paths:

      - Cucumber Test Using Gradle

  container:

    workingDir: /data/repo/Cucumber Test Using Gradle

  steps:

  - condition: always

    artifacts:

      paths:

      - '**/*'

```

The yaml file is self-explanatory as it lists down the details you’ve provided in the yaml. Below is the Cucumber feature file that explains what we are testing.

```feature

Feature: Hello World

  Scenario: Print Hello World

Given I have a working hello world example

When I run the hello world example

Then I should see "Hello World" on the console

```

The repo contains other files, including test steps and a test runner, which contain related code for executing 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. 

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

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.

This was a straightforward demo of creating a Cucumber 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

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.

About Testkube

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!