DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Spring Boot: JUnit Integration Test
  • Frequently Used Annotations in Spring Boot Applications
  • How to Create a Kubernetes Cluster on AWS With Jenkins and Spring Boot
  • SOAP Web Services With Apache CXF and Spring Boot

Trending

  • Navigating Software Leadership in a Dynamic Era
  • Mastering Backpressure in Java: Concepts, Real-World Examples, and Implementation
  • Instant Microservices: Rules for Logic and Security
  • JBang: How to Script With Java for Data Import From an API
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. Spring Boot Integration Test With Cucumber and Jenkins Pipeline

Spring Boot Integration Test With Cucumber and Jenkins Pipeline

Learn how to integrate the Cucumber framework with Spring Boot integration tests and collect reports in a Jenkins pipeline for behavior-driven testing.

Mahmoud Romeh user avatar by
Mahmoud Romeh
·
Updated Dec. 15, 17 · Tutorial
Like (12)
Save
Tweet
Share
51.5K Views

Join the DZone community and get the full member experience.

Join For Free

Here I am sharing how you can integrate Cucumber for behavior driven testing with Spring Boot integration tests, and how you collect the reports in Jenkins pipeline.

In a sample Spring Boot app generated from my custom Spring Boot archetype, we will show a small integration test suite with Cucumber and Spring Boot.

Steps to Follow

1- Add Cucumber maven dependencies to your Spring Boot pom.xml

 <!-- Cucumber-->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber-version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-spring</artifactId>
            <version>${cucumber-version}</version>
            <scope>test</scope>
        </dependency>

2- Define Cucumber features in your test resources:

Screen Shot 2017-12-03 at 19.00.06

3- How to define the features implementation to be executed with your Spring Boot app logic:

  • The feature description:

Feature: the health can be retrieved
  Scenario: client makes call to GET /health
    When the client calls /health
Then the client receives response status code of 200


  • The feature implementation:

/**
 * how the feature is executed
 */
public class GetHealthStep extends CucumberRoot {
    private ResponseEntity<String> response; // output

    @When("^the client calls /health$")
    public void the_client_issues_GET_health() throws Throwable {
        response = template.getForEntity("/health", String.class);
    }

    @Then("^the client receives response status code of (\\d+)$")
    public void the_client_receives_status_code_of(int statusCode) throws Throwable {
        HttpStatus currentStatusCode = response.getStatusCode();
        assertThat("status code is incorrect : " +
                response.getBody(), currentStatusCode.value(), is(statusCode));
    }

}

4- How to execute the integration test:

You need to configure the root executor with Cucumber runner as the following:

/**
 * the main class for cucumber where you configure where the features are defined and which formats of reports needed to be generated
 */
@RunWith(Cucumber.class)
@CucumberOptions(features = {"src/test/resources/features"}, format = {"pretty", "html:target/reports/cucumber/html",
        "json:target/cucumber.json", "usage:target/usage.jsonx", "junit:target/junit.xml"})
public class CucumberIntegrationIT {
}

The integration test triggering which will be done via spring boot integration test:

/**
 * main spring boot integration test with integration test profile
 */
@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("INTEGRATION_TEST")
@ContextConfiguration
public class CucumberRoot {

    @Autowired
    protected TestRestTemplate template;

    @Before
    public void before() {
        // demo to show how to add custom header Globally for the http request in spring test template , like  user header
        template.getRestTemplate().setInterceptors(Collections.singletonList((request, body, execution) -> {
            request.getHeaders()
                    .add("userHeader", "user");
            return execution.execute(request, body);
        }));
    }
}

5- How to collect the test reports in the Jenkins pipeline: 

stage('Integration tests') {
            // Run the maven build
            steps {
                script {
                    def mvnHome = tool 'Maven 3.3.9'
                    if (isUnix()) {
                      // to skip unit testing execution
                        sh "'${mvnHome}/bin/mvn'  verify -Dunit-tests.skip=true"
                    } else {
                        bat(/"${mvnHome}\bin\mvn" verify -Dunit-tests.skip=true/)
                    }

                }
              // here cucumber extension will collect the reports for you
                cucumber buildStatus: null, fileIncludePattern: '**/cucumber.json', jsonReportDirectory: 'target', sortingMethod: 'ALPHABETICAL'
            }
        }


The complete working sample is here:

GitHub: https://github.com/Romeh/spring-boot-sample-app

References:

  1. Cucumber: https://cucumber.io/
  2. Spring Boot: https://projects.spring.io/spring-boot/
Spring Framework Spring Boot integration test Cucumber (software) Pipeline (software) Jenkins (software)

Published at DZone with permission of Mahmoud Romeh, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot: JUnit Integration Test
  • Frequently Used Annotations in Spring Boot Applications
  • How to Create a Kubernetes Cluster on AWS With Jenkins and Spring Boot
  • SOAP Web Services With Apache CXF and Spring Boot

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: