Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

NPI – Spring Top – Temp – Non-Geo (Lightrun)

Get started with Spring 5 and Spring Boot 2, through the reference Learn Spring course:

NPI – Lightrun – Spring (partner)

We rely on other people’s code in our own work. Every day. It might be the language you’re writing in, the framework you’re building on, or some esoteric piece of software that does one thing so well you never found the need to implement it yourself.

The problem is, of course, when things fall apart in production - debugging the implementation of a 3rd party library you have no intimate knowledge of is, to say the least, tricky. It’s difficult to understand what talks to what and, specifically, which part of the underlying library is at fault.

Lightrun is a new kind of debugger.

It's one geared specifically towards real-life production environments. Using Lightrun, you can drill down into running applications, including 3rd party dependencies, with real-time logs, snapshots, and metrics. No hotfixes, redeployments, or restarts required.

Learn more in this quick, 5-minute Lightrun tutorial:

>> The Essential List of Spring Boot Annotations and Their Use Cases

1. Overview

Cucumber is a very powerful testing framework written in the Ruby programming language, which follows the BDD (behavior-driven development) methodology. It enables developers to write high-level use cases in plain text that can be verified by non-technical stakeholders, and turn them into executable tests, written in a language called Gherkin.

We have already discussed these in a different article.

And the Cucumber-Spring Integration is intended to make test automation easier. Once we have the Cucumber tests integrated with Spring, we should be able to execute them along with the Maven build.

2. Maven Dependencies

Let's get started using the Cucumber-Spring integration by defining the Maven dependencies – starting with the Cucumber-JVM dependency:


We can find the most recent version of Cucumber JVM here.

Next, we'll add the JUnit and Cucumber testing dependency:


The most recent version of Cucumber JUnit can be found here.

And finally, the Spring and Cucumber dependency:


Again, we can check out the most recent version of Cucumber Spring over here.

3. Configuration

We'll now look at how we can integrate Cucumber in a Spring application.

First, we'll create a Spring Boot application – for which we'll follow the Spring-Boot application article. Then, we'll create a Spring REST service and write the Cucumber test for it.

3.1. REST Controller

First, let's create a simple controller:

public class VersionController {
    public String getVersion() {
        return "1.0";

3.2. Cucumber Step Definitions

All we need to run our Cucumber tests with JUnit is to create a single empty class with an annotation @RunWith(Cucumber.class):

@CucumberOptions(features = "src/test/resources")
public class CucumberIntegrationTest {

We can see the annotation @CucumberOptions where we're specifying the location of the Gherkin file which is also known as the feature file. At this point, Cucumber recognizes the Gherkin language; you can read more about Gherkin in the article mentioned in the introduction.

So now, let's create a Cucumber feature file:

Feature: the version can be retrieved
  Scenario: client makes call to GET /version
    When the client calls /version
    Then the client receives status code of 200
    And the client receives server version 1.0

The Scenario is to make a GET call to the REST service url /version and verify the response.

Next, we need to create a so-called glue code. These are methods that link a single Gherkin step with Java code.

We have to options here – we can either use Cucumber Expressions or regular expressions inside the annotations. In our case, we'll stick to the regular expressions:

@When("^the client calls /version$")
public void the_client_issues_GET_version() throws Throwable{

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

@And("^the client receives server version (.+)$")
public void the_client_receives_server_version_body(String version) throws Throwable {
    assertThat(latestResponse.getBody(), is(version));

So now let's integrate the Cucumber tests with the Spring Application Context. For that, we'll create a new class and annotate it with @SpringBootTest and @CucumberContextConfiguration:

public class SpringIntegrationTest {
    // executeGet implementation

Now all the Cucumber definitions can go into a separate Java class which extends SpringIntegrationTest:

public class StepDefs extends SpringIntegrationTest {
    @When("^the client calls /version$")
    public void the_client_issues_GET_version() throws Throwable {

We are all set for a test run now.

Finally, we can do a quick run via the command line, simply running mvn clean install -Pintegration-lite-first – Maven will execute the integration tests and show the results in the console.

3 Scenarios ([32m3 passed[0m)
9 Steps ([32m9 passed[0m)

Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 9.283 sec - in
2016-07-30 06:28:20.142  INFO 732 --- [Thread-2] AnnotationConfigEmbeddedWebApplicationContext :
  Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext:
  startup date [Sat Jul 30 06:28:12 CDT 2016]; root of context hierarchy

Results :

Tests run: 12, Failures: 0, Errors: 0, Skipped: 0

4. Conclusion

Having configured Cucumber with Spring, it will be handy to use Spring-configured components in BDD testing. This is a simple guide for integrating the Cucumber test in a Spring-Boot application.

As usual, all code samples shown in this tutorial are available over on GitHub.

Spring bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Junit footer banner
Comments are closed on this article!