It’s an apparent reality for anybody who’s been utilizing Cucumber for Java in check automation that steps have to be outlined inside a category. Passing check state from one step definition to a different will be simply achieved utilizing occasion variables, however that solely works for elementary and small tasks. In any scenario the place writing cucumber eventualities is a part of a non-trivial software program supply endeavor, Dependency Injection (DI) is the popular (and often vital!) resolution. After studying the article beneath, you’ll be taught why that’s the case and easy methods to implement DI in your Cucumber-JVM checks shortly.
Let’s take a look on the following state of affairs written in Gherkin:
If we assume that it’s a part of a small check suite, then its implementation utilizing step definitions inside the Cucumber-JVM framework may appear to be this:
Within the instance above, the info is handed between step definitions (strategies) via occasion variables. This works as a result of the strategies are in the identical class – PurchaseProcess, since occasion variables are typically accessible solely inside the identical class that declares them.
The variety of step definitions grows when the variety of Cucumber eventualities grows. Eventually, this forces us to separate our steps into a number of lessons – to keep up code readability and maintainability, amongst different causes. Making use of this truism to the earlier instance would possibly end in one thing like this:
However now we face an issue: the checkPriceInHistory methodology moved into the newly created PurchaseHistory class can’t freely entry information saved in occasion variables of its authentic PurchaseProcess class.
So how can we go about fixing this pickle? The reply is Dependency Injection (DI) – the really useful means of sharing the state between steps in Cucumber-JVM.
For those who’re unfamiliar with this idea, then go by Wikipedia’s definition:
“In software engineering, dependency injection is a design pattern by which an object or function receives different objects or capabilities that it relies on. A type of inversion of control, dependency injection goals to separate the concerns of developing and utilizing objects, resulting in loosely coupled applications. The sample ensures that an object or perform which desires to make use of a given service shouldn’t must know easy methods to assemble these companies. As an alternative, the receiving ‘client‘ (object or perform) is supplied with its dependencies by exterior code (an ‘injector’), which it isn’t conscious of.” 
Within the context of Cucumber, to make use of dependency injection is to “inject a typical object in every class with steps. An object that’s recreated each time a brand new state of affairs is executed.” 
Thus Comes PicoContainer
JVM implementation of Cucumber helps a number of DI modules: PicoContainer, Spring, Guice, OpenEJB, Weld, and Needle. PicoContainer is really useful in case your utility doesn’t already use one other one. 
The principle advantages of utilizing PicoContainer over different DI modules steam from it being tiny and easy:
- It doesn’t require any configuration
- It doesn’t require your lessons to make use of any APIs
- It solely has a single characteristic – it instantiates objects 
To make use of PicoContainer with Maven, add the next dependency to your pom.xml:
<dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-picocontainer</artifactId> <model>7.8.1</model> <scope>check</scope> </dependency>
If utilizing Gradle, add:
compile group: 'io.cucumber', identify: 'cucumber-picocontainer', model: ‚7.8.1’
To your construct.gradle file.
Now let’s return to our instance code. The implementation of DI utilizing PicoContainer is fairly easy. First, we’ve got to create a container class that can maintain the widespread information:
Then we have to add a constructor injection to implement the PurchaseProcess and PurchaseHistory lessons. This boils right down to the next:
- making a reference variable of the Container class within the present step lessons
- initializing the reference variable via a constructor
As soon as the adjustments above are utilized, the instance ought to appear to be this:
PicoContainer is light-weight and straightforward to implement. It additionally requires minimal adjustments to your current code, serving to to maintain it lean and readable. These qualities make it an ideal match for any Cucumber-JVM challenge since sharing check context between lessons is a query of ‘when’ and never ‘if’ in primarily any check suite that can develop past a number of eventualities.