I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

Cucumber is a very powerful testing framework written in the Ruby programming language, which follows the BDD (behavior-driven development) methodology. Its intent is to enable 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:

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.4</version>
    <scope>test</scope>
</dependency>

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

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

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>1.2.4</version>
    <scope>test</scope>
</dependency>

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

And finally, the Spring and Cucumber dependency:

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-spring</artifactId>
    <version>1.2.4</version>
    <scope>test</scope>
</dependency>

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

3. Configuration

We’ll now look at how we can integrate Cucumber in a Spring Micro service application. The first step is to create a Spring Boot application – for which we’ll follow the Spring-Boot application article.

Then we’ll create a Spring REST service in the Boot application and write the Cucumber test for this REST Service.

3.1. REST Controller

As a first step let us create a controller class for a simple REST API:

@RestController
public class VersionController {
    @RequestMapping(method={RequestMethod.GET},value={"/version"})
    public String getVersion() {
        return "1.0";
    }
}

3.2. Cucumber Step Definitions

The JUnit runner uses the JUnit framework to run the Cucumber Test. All we need is to create a single empty class with an annotation @RunWith(Cucumber.class):

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources")
public class CucumberTest {
}

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 us 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

And let’s take a look at the feature file. The Scenario is to make a GET call to the REST service url /version and assert the response.

The next step is to create the method in the Java class to correspond to this test case:

@When("^the client calls /version$")
public void the_client_issues_GET_version() throws Throwable{
    executeGet("http://localhost:8080/version");
}

@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 we need to execute these Cucumber test case methods using Maven and Spring. Create a class which can run with Spring-JUnit so that Maven can execute those test class:

@ContextConfiguration(
  classes = SpringDemoApplication.class, 
  loader = SpringApplicationContextLoader.class)
@WebAppConfiguration
@IntegrationTest
public class SpringIntegrationTest {

}

Now all the Cucumber definitions can go into a separate Java class which extends to the Java class mentioned above:

public class StepDefs extends SpringIntegrationTest {
   
    @When("^the client calls /version$")
    public void the_client_issues_GET_version() throws Throwable {
        executeGet("http://localhost:8080/version");
    }
}

We are all set for a test run now.

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

3 Scenarios ([32m3 passed[0m)
9 Steps ([32m9 passed[0m)
0m1.054s

Tests run: 12, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 9.283 sec - in
  com.baeldung.CucumberTest
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 Cucumber test in a Spring-Boot application.

You can find an example project based on the code in this article in the linked GitHub repository.

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS

newest oldest most voted
Notify of
James Haugen
Guest
James Haugen

hello, do you have example for setting up cucumber, TestNg, spring, and mockito?

Eugen Paraschiv
Guest

Hey James,
That’s an interesting question – there’s no article that covers all of them at once, but there are individual pieces about: Cucumber, and Cucumber with Spring, TestNG, Mockito.

Hope that helps. Cheers,
Eugen.

Ankit
Guest
Ankit

Could you help with definition of method executeGet?

Grzegorz Piwowarek
Guest
Grzegorz Piwowarek