Skip to content

Commit

Permalink
Add documentation for spring object factory #1405
Browse files Browse the repository at this point in the history
Add documentation for spring object factory
  • Loading branch information
mlvandijk authored and mpkorstanje committed Jun 29, 2018
1 parent b9a41b0 commit cf043f1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
2 changes: 1 addition & 1 deletion junit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Cucumber JUnit

Use JUnit to execute cucumber scenarios.

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

```xml
<dependencies>
Expand Down
93 changes: 93 additions & 0 deletions spring/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Cucumber Spring
===============

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

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

```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 the `@Component` annotation to each of the classes cucumber-spring should manage.

* Add a `@Scope("cucumber-glue")` annotation to have cucumber-spring remove them **after each scenario**

* Add the location of your classes to the `@ComponentScan` of your (test) configuration:

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

@Configuration
@ComponentScan("your.package")
public class Config {
// the rest of your configuration
}
```

For classes from other frameworks:

* You will have to explicitly register them as Beans in your (test) configuration:

```java
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("your.package")
public class Config {
@Bean
@Scope("cucumber-glue")
public Class otherClass() {
// return an instance of the other class
}
}
```

Now you can use the registered Beans by Autowiring them where you need them.

For example:
```java
import your.package.OtherStepDefs;
import org.springframework.beans.factory.annotation.Autowired;

public class StepDefs {
@Autowired
OtherStepDefs otherStepDefs;

// 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:

```xml
<bean class="your.package.YourClass" scope="cucumber-glue" />
<bean class="other.framework.Class" scope="cucumber-glue" />
```

Annotate your StepDefinition class with `@ContextConfiguration("classpath:cucumber.xml")`

## SpringBoot

If you are using SpringBoot, you can annotate your StepDefinition class with `@SpringBootTest(classes = TestConfig.class)`.

0 comments on commit cf043f1

Please sign in to comment.