Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
src [Core] Formalize glue path Feb 3, 2019
README.md Update spring documentation (#1441) Sep 1, 2018
pom.xml

README.md

Cucumber Spring

Use Cucumber Spring to manage state between steps and for scenarios.

Add the cucumber-spring dependency to your pom.xml:

<dependencies>
  [...]
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-spring</artifactId>
        <version>${cucumber.version}</version>
        <scope>test</scope>
    </dependency>
  [...]
</dependencies>

Annotation Based Configuration

For your own classes:

  • Add a @Component annotation to each of the classes cucumber-spring should manage.
  • Add the location of your classes to the @ComponentScan of your (test) configuration:
package com.example.app;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.example.app")
public class Config {
    // the rest of your configuration
}
  • Add @DirtiesContext to your test configuration if each scenario should have a fresh application context.

For classes from other frameworks:

  • You will have to explicitly register them as Beans in your (test) configuration:
package com.example.app;

import other.framework.Class;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
@ComponentScan("com.example.app")
public class TestConfig {
    @Bean
    public SomeOtherService someOtherService() {
        // return an instance of some other service
    }
}

To make Cucumber aware of your test configuration you can annotate a single step definition with @ContextConfiguration, @ContextHierarchy or @BootstrapWith. If you are using SpringBoot, you can annotate a single step definition class with @SpringBootTest(classes = TestConfig.class).

For example:

import com.example.app;
import org.springframework.beans.factory.annotation.Autowired;

@SpringBootTest(classes = TestConfig.class)
public class SomeServiceSteps {

    // the rest of your step definitions

}

Now you can use the registered beans by autowiring them where you need them.

For example:

import com.example.app;
import org.springframework.beans.factory.annotation.Autowired;

@SpringBootTest(classes = TestConfig.class)
public class SomeServiceSteps {
    
    @Autowired
    SomeService someService;

    @Autowired
    SomeOtherService someOtherService;

    // the rest of your step definitions
}

XML Configuration

If you are using xml based configuration, you can to register the beans in a cucumber.xml file:

<bean class="com.example.app.MyService"/>
<bean class="com.example.lib.SomeOtherService"/>

Annotate a single step definition class with @ContextConfiguration("classpath:cucumber.xml")

You can’t perform that action at this time.