|
| 1 | +Cucumber Spring |
| 2 | +=============== |
| 3 | + |
| 4 | +Use Cucumber Spring to manage state between steps and for scenarios. |
| 5 | + |
| 6 | +Add the `cucumber-spring` dependency to your pom.xml: |
| 7 | + |
| 8 | +```xml |
| 9 | +<dependencies> |
| 10 | + [...] |
| 11 | + <dependency> |
| 12 | + <groupId>io.cucumber</groupId> |
| 13 | + <artifactId>cucumber-spring</artifactId> |
| 14 | + <version>${cucumber.version}</version> |
| 15 | + <scope>test</scope> |
| 16 | + </dependency> |
| 17 | + [...] |
| 18 | +</dependencies> |
| 19 | +``` |
| 20 | + |
| 21 | +## Annotation Based Configuration |
| 22 | + |
| 23 | +For your own classes: |
| 24 | + |
| 25 | +* Add the `@Component` annotation to each of the classes cucumber-spring should manage. |
| 26 | + |
| 27 | +* Add a `@Scope("cucumber-glue")` annotation to have cucumber-spring remove them **after each scenario** |
| 28 | + |
| 29 | +* Add the location of your classes to the `@ComponentScan` of your (test) configuration: |
| 30 | + |
| 31 | +```java |
| 32 | +import org.springframework.context.annotation.ComponentScan; |
| 33 | +import org.springframework.context.annotation.Configuration; |
| 34 | + |
| 35 | +@Configuration |
| 36 | +@ComponentScan("your.package") |
| 37 | +public class Config { |
| 38 | + // the rest of your configuration |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +For classes from other frameworks: |
| 43 | + |
| 44 | +* You will have to explicitly register them as Beans in your (test) configuration: |
| 45 | + |
| 46 | +```java |
| 47 | +import other.framework.Class; |
| 48 | +import org.springframework.context.annotation.Bean; |
| 49 | +import org.springframework.context.annotation.ComponentScan; |
| 50 | +import org.springframework.context.annotation.Configuration; |
| 51 | +import org.springframework.context.annotation.Scope; |
| 52 | + |
| 53 | +@Configuration |
| 54 | +@ComponentScan("your.package") |
| 55 | +public class Config { |
| 56 | + @Bean |
| 57 | + @Scope("cucumber-glue") |
| 58 | + public Class otherClass() { |
| 59 | + // return an instance of the other class |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +Now you can use the registered Beans by Autowiring them where you need them. |
| 65 | + |
| 66 | +For example: |
| 67 | +```java |
| 68 | +import your.package.OtherStepDefs; |
| 69 | +import org.springframework.beans.factory.annotation.Autowired; |
| 70 | + |
| 71 | +public class StepDefs { |
| 72 | + @Autowired |
| 73 | + OtherStepDefs otherStepDefs; |
| 74 | + |
| 75 | + // the rest of your step definitions |
| 76 | +} |
| 77 | + |
| 78 | +``` |
| 79 | + |
| 80 | +## XML Configuration |
| 81 | + |
| 82 | +If you are using xml based configuration, you can to register the beans in a `cucumber.xml` file: |
| 83 | + |
| 84 | +```xml |
| 85 | +<bean class="your.package.YourClass" scope="cucumber-glue" /> |
| 86 | +<bean class="other.framework.Class" scope="cucumber-glue" /> |
| 87 | +``` |
| 88 | + |
| 89 | +Annotate your StepDefinition class with `@ContextConfiguration("classpath:cucumber.xml")` |
| 90 | + |
| 91 | +## SpringBoot |
| 92 | + |
| 93 | +If you are using SpringBoot, you can annotate your StepDefinition class with `@SpringBootTest(classes = TestConfig.class)`. |
0 commit comments