|
| 1 | +Cucumber DeltaSpike |
| 2 | +=================== |
| 3 | + |
| 4 | +This module relies on [DeltaSpike Container Control](https://deltaspike.apache.org/documentation/container-control.html) to start/stop supported CDI container. |
| 5 | + |
| 6 | +## Setup |
| 7 | +Enable cdi support for your steps by adding a (empty) beans.xml into your classpath (src/main/resource/META-INF for normal classes or src/test/resources/META-INF for test classes): |
| 8 | + |
| 9 | +```xml |
| 10 | +<beans xmlns="http://java.sun.com/xml/ns/javaee" |
| 11 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 12 | + xsi:schemaLocation=" |
| 13 | + http://java.sun.com/xml/ns/javaee |
| 14 | + http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> |
| 15 | + |
| 16 | +</beans> |
| 17 | +``` |
| 18 | + |
| 19 | +To use DependencyInjection add `@Inject` to any field which should be managed by CDI, for more information see [JSR330](https://www.jcp.org/en/jsr/detail?id=330). |
| 20 | + |
| 21 | +```java |
| 22 | +public class BellyStepdefs { |
| 23 | + |
| 24 | + @Inject |
| 25 | + private Belly belly; |
| 26 | + |
| 27 | + //normal step code ... |
| 28 | +``` |
| 29 | + |
| 30 | +This ObjectFactory doesn't start or stop any [Scopes](https://docs.oracle.com/javaee/6/tutorial/doc/gjbbk.html), so all beans live inside the default scope (Dependent). Now cucumber requested a instance of your stepdefinitions for every step, which means cdi create a new instance for every step and for all injected fields. This behaviour makes it impossible to share a state inside a szenario. |
| 31 | + |
| 32 | +To bybass this, you must annotate your class(es) with `@javax.inject.Singleton`: |
| 33 | +1. on stepdefintions: now the ojectfactory will creates only one instance include injected fields per scenario and both injected fields and stepdefinitions can be used to share state inside a scenario. |
| 34 | +2. on any other class: now the objectfactory will create a new instance of your stepdefinitions per step and stepdefinitions can not be used to share state inside a scenario, only the annotated classes can be used to share state inside a scenario |
| 35 | + |
| 36 | +you can also combine both approaches. |
| 37 | + |
| 38 | +```java |
| 39 | +@Singleton |
| 40 | +public class BellyStepdefs { |
| 41 | + |
| 42 | + @Inject |
| 43 | + private Belly belly; |
| 44 | + |
| 45 | + //normal step code ... |
| 46 | +``` |
| 47 | +It is not possible to use any other scope than Dependent this means alsoi it is not possible to share a state over two or more scenarios, every scenario start with a clean environment. |
| 48 | + |
| 49 | +To enable this objectfactory add the folling dependency to your classpath: |
| 50 | +```xml |
| 51 | +<dependency> |
| 52 | + <groupId>io.cucumber</groupId> |
| 53 | + <artifactId>cucumber-deltaspike</artifactId> |
| 54 | + <version>${cucumber.version}</version> |
| 55 | + <scope>test</scope> |
| 56 | +</dependency> |
| 57 | +``` |
| 58 | + |
| 59 | +and one of the supported cdi-containers. |
| 60 | + |
| 61 | +to use it with Weld: |
| 62 | + |
| 63 | +```xml |
| 64 | +<dependency> |
| 65 | + <groupId>org.apache.deltaspike.cdictrl</groupId> |
| 66 | + <artifactId>deltaspike-cdictrl-weld</artifactId> |
| 67 | + <version>${deltaspike.version}</version> |
| 68 | + <scope>test</scope> |
| 69 | +</dependency> |
| 70 | +<dependency> |
| 71 | + <groupId>org.jboss.weld.se</groupId> |
| 72 | + <artifactId>weld-se-core</artifactId> |
| 73 | + <version>${weld-se.version}</version> |
| 74 | + <scope>test</scope> |
| 75 | +</dependency> |
| 76 | +``` |
| 77 | + |
| 78 | +or to use it with OpenEJB: |
| 79 | + |
| 80 | +```xml |
| 81 | +<dependency> |
| 82 | + <groupId>org.apache.deltaspike.cdictrl</groupId> |
| 83 | + <artifactId>deltaspike-cdictrl-openejb</artifactId> |
| 84 | + <version>${deltaspike.version}</version> |
| 85 | + <scope>test</scope> |
| 86 | +</dependency> |
| 87 | +<dependency> |
| 88 | + <groupId>org.apache.tomee</groupId> |
| 89 | + <artifactId>openejb-core</artifactId> |
| 90 | + <version>${openejb-core.version}</version> |
| 91 | + <scope>test</scope> |
| 92 | +</dependency> |
| 93 | +<dependency> |
| 94 | + <groupId>javax.xml.bind</groupId> |
| 95 | + <artifactId>jaxb-api</artifactId> |
| 96 | + <version>${jaxb-api.version}</version> |
| 97 | + <scope>test</scope> |
| 98 | +</dependency> |
| 99 | +<dependency> |
| 100 | + <groupId>org.glassfish.jaxb</groupId> |
| 101 | + <artifactId>jaxb-runtime</artifactId> |
| 102 | + <version>${jaxb-api.version}</version> |
| 103 | + <scope>test</scope> |
| 104 | +</dependency> |
| 105 | +``` |
| 106 | + |
| 107 | +or to use it with OpenWebBeans: |
| 108 | +```xml |
| 109 | +<dependency> |
| 110 | + <groupId>org.apache.deltaspike.cdictrl</groupId> |
| 111 | + <artifactId>deltaspike-cdictrl-owb</artifactId> |
| 112 | + <version>${deltaspike.version}</version> |
| 113 | + <scope>test</scope> |
| 114 | +</dependency> |
| 115 | +<dependency> |
| 116 | + <groupId>org.apache.openwebbeans</groupId> |
| 117 | + <artifactId>openwebbeans-impl</artifactId> |
| 118 | + <version>${owb.version}</version> |
| 119 | + <scope>test</scope> |
| 120 | +</dependency> |
| 121 | +<dependency> |
| 122 | + <groupId>javax.annotation</groupId> |
| 123 | + <artifactId>jsr250-api</artifactId> |
| 124 | + <version>1.0</version> |
| 125 | + <scope>test</scope> |
| 126 | +</dependency> |
| 127 | +``` |
| 128 | + |
| 129 | +Some containers need that you provide a CDI-API in a given version, but if you develop CDI and use one of the above containers it should already on your path. |
0 commit comments