Cucumber-JVM: Injection example

3,192 views
Skip to first unread message

Robert

unread,
Sep 8, 2011, 2:09:44 PM9/8/11
to cu...@googlegroups.com
I'm looking for some help with injection...particularly related to the cucumber-picocontainer artifact.  Can someone please provide me with an example of how to use injection?  Thanks.

Bill Ross

unread,
Sep 8, 2011, 2:29:41 PM9/8/11
to cu...@googlegroups.com
> I'm looking for some help with injection...particularly related to the
> cucumber-picocontainer artifact. Can someone please provide me with an
> example of how to use injection? Thanks.

For what it's worth, here's how I inject the context class using Spring
with cuke4duke:

Spring:

<bean class="com.mycompany.test.features.StepContext" />

Java:

@StepDefinitions
public class PPSteps {

@Autowired
private StepContext stepContext;

Bill

Robert

unread,
Sep 8, 2011, 4:34:15 PM9/8/11
to cu...@googlegroups.com

Thanks, Bill.  It looks relatively straight forward to use.  Anybody out there have an example associated with picocontainer?

John Lonergan

unread,
Sep 8, 2011, 7:42:05 PM9/8/11
to cu...@googlegroups.com
For various reasons I have found it useful to organize my test framework as follows.

I organize my cuke steps into a collection of small classes of related functions.
We have a separate class (or set of classes) that record any state thats shared across the step classes.
We let pico constructor injection take care of the injection of the state classes into the step classes.

We wrap a thin skin around the app providing a high level facade (we call it an AppRunner) that the cuke steps interact with.
The app under test uses spring.

When the pico container boots up it instantiates our facade class which in turn loads the spring application context of the application under test, in the process this instantiates the app under test.
The facade class needs to talk to various components in the app context.
To get handles on those objects we use @Resource on the facade.
The final step is to dependency inject the facade class itself to populate the @Resources's.
To achieve this we do
 applicationContext.getAutowireCapableBeanFactory().autowireBean(facade);

This approach helped us organize the step's nicely and decouples the app boot from the cuke container.

JL

patrickmcmichael

unread,
Sep 10, 2011, 9:08:25 PM9/10/11
to Cukes
Having stumbled on cuke4duke after it had become deprecated in favor
of cucumber-jvm, I had a bit more manual work to do, but as of this
afternoon, have spring injection playing nicely. I did a local
conversion of the java-calculator example that works via Spring.

Basic steps:

1) Swap spring for pico container (test scope caused transitive
dependency fun w/ spring framework stuff, so went back to default
scope!):

<!--
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>${project.version}</version>
<!-- <scope>test</scope> -->
</dependency>

2) Add a cucumber.xml in src/test/resources:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="cucumber.examples.java"/>
</beans>


3) Replaced inline instantiation of RpnCalculator in two stepdef java
files to use Spring @Autowired:

@Autowired
private RpnCalculator calc;


4) Set up RpnCalculator for detection by spring context scanning (note
prototype scope...w/o that, Shopping steps fail due to the stageful
nature of RpnCalculator and leftover data from prior scenarios!)

@Component
@Scope("prototype")
public class RpnCalculator
.
.
.


With those pieces in play, it works like a charm, running features
thru JUnit (in Eclipse...nice feature/scenario output) or through
command line mvn test call (less helpful/self-documenting output).



Enjoy!

PWM
Reply all
Reply to author
Forward
0 new messages