I am currently trying to integrate Spring into my Cucumber tests. I have a custom SpringFactory as outlined in:
http://www.zsoltfabok.com/blog/2012/01/cucumber-jvm-di/
but the code is not being called. What is the best way to 'hook up' the factory with my tests?
[Clarification from comment on deleted answer: the main issue is that my test has Cucumber as the main runner, i.e. @RunWith(Cucumber.class) so cannot use the SpringJUnit4ClassRunner here. This is described in the link above but there is no description of how cucumber.xml is read. I have added a new SpringFactory but it isnt reading the XML file....]
Update: Actually have got a little further by adding:
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>1.0.0.RC15</version>
</dependency>
However, I have @Autowired variables which are not being injected as the test is being run by Cucumber.
Has anyone else had this problem?
We'll now look at how we can integrate Cucumber in a Spring application. First, we'll create a Spring Boot application – for which we'll follow the Spring-Boot application article. Then, we'll create a Spring REST service and write the Cucumber test for it.
This means that all parts of the agile testing pyramid can be implemented using Cucumber. You can can implement end-to-end tests, integration tests, and parts that could be tested using unit tests. The decision to use Cucumber or a unit testing framework is depending on the cooperation with the business.
Update: The cuke4duke project is now obsolete and has been replaced by Cucumber-JVM.
I also had the same issue and the root cause was that cucumber.xml was not being read by the SpringFactory in cucumber-spring-1.1.5.jar. Spring DI failed silently resulting in NPEs.
Since submission 448, the Cucumber SpringFactory no longer loads cucumber.xml and you are required to include @ContextConfiguration("classpath*:cucumber.xml")
in ALL your step definition classes.
A quick Google search for "cucumber spring integration" brings me here and here. If you are using cuke4duke
this looks like a much simpler way to go.
The sample seems to indicate that, by default, cuke4duke
will load cucumber.xml
from the base of your classpath. This is a Spring configuration file which must include a component-scan
for classes annotated with @StepDefinition
.
You will not need to provide a SpringFactory
, you will not need to use the @RunWith
annotation, just provide some Spring configuration and one or more jvm properties.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With