1. Overview
This is a quick guide to integrate the Cucumber testing framework with Spring.
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.
2.Step by step tutorial
Assume we need to integrate functional from our spring application with Cucumber. A strong feature of spring is Dependency Injection, for this we will create a fast project with a simple functional and integrate this functional to use in out test for integration tests of for system testing.
2.1 Creating a project
We will create a project with help of IntellIJ and with the following arhitecture:
2.2 Adding maven dependency to integrate with cucumber
After this we will add maven dependency to integrate with our new project:
<dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-core</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-spring</artifactId> <version>1.2.5</version> <scope>test</scope> </dependency>
Assume we have an service or more services in our spring application and we want to use it in our test scenarios and cases.For this we created a fast service in our application:
And created a runner for our tests in test folder:
In our step definition we injected our service:
But when we run test we receive a NullPointerException, how we can solve this and why this happens ?! This happend becouse we don’t initiate our service, for this we will need to create a configuration class. So here is our configuration class:
A simple configuration class what will scan our application to found services or components. And after this we will modify our step definition class to use this configuration. Bellow is show the modified step definition class:
After this you will not get more NullPointerException and you can easy use your spring functional for testing. Enjoy and hope this will help you.