How to integrate Spring into Cucumber

2019-03-27 05:35发布

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?

4条回答
放荡不羁爱自由
2楼-- · 2019-03-27 06:09

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.

查看更多
Melony?
3楼-- · 2019-03-27 06:10

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.

查看更多
时光不老,我们不散
4楼-- · 2019-03-27 06:26

The problem is that certain versions of Spring only work with certain versions of Cucumber-Spring and Cucumber-JVM. Finding the winning combination can take two days fiddling with jar versions. So start with a "Hello World" BDD scenario that uses simple injection that you know will work, because you'll need to tweak those jars until you get an environment that works.

The easiest way is to match the latest version of Spring with the latest version of Cucumber-Spring. The new versions of Spring are better supported. If you can't simply upgrade your spring, then use websites that describe maven dependencies between different versions of Cucumber-Spring and the version of Spring you're working with.

If you want to stick with your legacy version of Spring (bad idea, the pain of changing will only grow worse) another strategy is to use JBehave for BDD testing.

查看更多
成全新的幸福
5楼-- · 2019-03-27 06:30

I've just seen your question. Most probably the cucumber-spring module dependency was missing. I've updated the repository and now all the dependencies should be fine (I'm using ivy).

查看更多
登录 后发表回答