Skip to content

Commit cf043f1

Browse files
mlvandijkmpkorstanje
authored andcommitted
Add documentation for spring object factory cucumber#1405
Add documentation for spring object factory
1 parent b9a41b0 commit cf043f1

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

junit/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Cucumber JUnit
33

44
Use JUnit to execute cucumber scenarios.
55

6-
Add the `cucumber-junit` dependency to your pom.
6+
Add the `cucumber-junit` dependency to your pom.xml:
77

88
```xml
99
<dependencies>

spring/README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)