Home / 

There is an ever-changing demand for testing tools that are less programmer-centric and better inclined towards a behavior-driven development (BDD) testing. This reduces the gap between software development and a testing environment. Cucumber provides the testing utility that is written in BDD style and helps in defining the system behavior according to the user’s perspective in simple English text called Gherkin. One can write test cases with plain syntax (like Given, When, and Then) to execute a single test scenario or multiple test scenarios. Cucumber, by default, is written in Ruby, but it can be extended to other programming languages, like JAVA, .NET, Groovy, etc.

Maven Dependencies

To enhance the efficiency of the test automation process, Cucumber test scenarios are integrated with Spring, which can be further executed through Maven. The first step towards Cucumber-Spring integration is to define the Maven dependencies – starting with Cucumber-Java Virtual Machine (JVM) dependency.

Maven Dependencies

Next, we will add the JUnit and Cucumber testing dependency:

Spring Dependency Injection

Cucumber-Spring Dependency Injection (DI)

Cucumber supports several DI frameworks, which include Pico Container, Spring, and Guice. Independent of programming language, the DI technique works on the principle of “handover the service” to a dependent object rather than finding a service. The workaround is to share the state to leverage the DI potential. Additionally, using DI with Cucumber has several benefits:

  • One can safely share the state anytime between the steps.
  • It helps in improving the design of the test code, making it easier to share the state between different step definition classes (that way, one can split them into meaningful groups).
  • It efficiently creates and manages instances of classes.

Mentioned below are the dependencies that can be used in a Cucumber framework for Spring DI Integration:

Spring Configuration File

Spring Configuration File:

Spring Annotations

Spring Annotations

@ComponentScan: With Spring, we use the @ComponentScan annotation along with @Configuration annotation to specify the packages we want to scan. @ComponentScan tells Spring to scan the current package and all its sub-packages without arguments.

@Primary: @Primary should be used in conjunction with @Bean / @Autowired. This would indicate which bean should be given higher preference when multiple beans of the same type are present.

@Qualifier: @Qualifier should be used in conjunction with @Autowired always. This would indicate the bean name that needs to be @Autowired in case of multiple beans with the same type are present in the application context (for the Springtoautowire by name).

@Component: This annotation is used in classes to indicate a Spring component. The @Component annotation marks the Java class as a bean (or say component) so that the component-scanning mechanism of Spring can be added into the application context.

@Configuration: This annotation is used in classes that define beans. @Configuration is an analog for the XML configuration file, which is configured using Java class. Java class annotated with @Configuration is a configuration by itself and will have methods to instantiate and configure the dependencies.

@Bean: This annotation is used at the method level. @Bean annotation works with @Configuration to create Spring beans. As mentioned earlier, @Configuration will have methods to instantiate and configure dependencies. Such methods will be annotated with @Bean. The method annotated with this annotation works as bean ID and it creates and returns the actual bean.

The Spring-Cucumber DI integration comes with several positive scenarios, such as:

  • Having configured Cucumber with Spring, it will be handy to use Spring-configured components in BDD testing.
  • For a large scale framework, Spring must be used for the code to be well managed and structured, which reduces the maintenance effort.
  • Spring also helps in pacing up test case execution on multiple platforms with minimal configuration.
  • Spring annotations, such as @Autowired, are the ones that help in binding all classes into one packet.