如何将 Spring 集成到 Cucumber 中
我目前正在尝试将 Spring 集成到我的 Cucumber 测试中。我有一个自定义 SpringFactory,如下所示:
http://www.zsoltfabok.com/blog/ 2012/01/cucumber-jvm-di/
但代码没有被调用。将工厂与我的测试“连接”的最佳方法是什么?
[对已删除答案的评论澄清:主要问题是我的测试以 Cucumber 作为主要运行器,即 @RunWith(Cucumber.class),因此不能在此处使用 SpringJUnit4ClassRunner。上面的链接对此进行了描述,但没有描述如何读取 cucumber.xml。我添加了一个新的 SpringFactory,但它没有读取 XML 文件...]
更新:实际上通过添加更进一步:
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>1.0.0.RC15</version>
</dependency>
但是,我有 @Autowired 变量,当 Cucumber 运行测试时,这些变量没有被注入。
还有其他人遇到过这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更新:cuke4duke 项目现已过时,已被 Cucumber-JVM 取代。
我也遇到了同样的问题,根本原因是 cucumber-spring-1.1.5.jar 中的 SpringFactory 没有读取 cucumber.xml。 Spring DI 默默失败,导致 NPE。
自提交448以来,Cucumber SpringFactory不再加载cucumber.xml,您需要包含所有步骤定义类中的
@ContextConfiguration("classpath*:cucumber.xml")
。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.在 Google 上快速搜索“cucumber spring Integration”,我会找到这里和此处。如果您使用的是 cuke4duke,这看起来是一种更简单的方法。
该示例似乎表明,默认情况下,
cuke4duke
将从类路径的基础加载cucumber.xml
。这是一个 Spring 配置文件,必须包含使用@StepDefinition
注释的类的component-scan
。您不需要提供
SpringFactory
,也不需要使用@RunWith
注解,只需提供一些 Spring 配置和一个或多个 jvm 属性。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 loadcucumber.xml
from the base of your classpath. This is a Spring configuration file which must include acomponent-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.我刚刚看到你的问题。很可能缺少
cucumber-spring
模块依赖项。我已经更新了存储库,现在所有依赖项都应该没问题(我正在使用 ivy)。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).问题是某些版本的 Spring 仅适用于某些版本的 Cucumber-Spring 和 Cucumber-JVM。找到获胜的组合可能需要花两天时间摆弄 jar 版本。因此,从“Hello World”BDD 场景开始,该场景使用您知道可以工作的简单注入,因为您需要调整这些 jar,直到获得一个可以工作的环境。
最简单的方法是将最新版本的 Spring 与最新版本的 Cucumber-Spring 相匹配。新版本的Spring得到了更好的支持。如果您不能简单地升级您的 spring,那么请使用描述不同版本的 Cucumber-Spring 和您正在使用的 Spring 版本之间的 Maven 依赖关系的网站。
如果您想坚持使用 Spring 的旧版本(坏主意,更改的痛苦只会变得更糟),另一个策略是使用 JBehave 进行 BDD 测试。
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.