如何将 Spring 集成到 Cucumber 中

发布于 2025-01-07 23:45:57 字数 772 浏览 3 评论 0原文

我目前正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

落花随流水 2025-01-14 23:45:57

更新: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.

安人多梦 2025-01-14 23:45:57

在 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 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.

狼性发作 2025-01-14 23:45:57

我刚刚看到你的问题。很可能缺少 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).

禾厶谷欠 2025-01-14 23:45:57

问题是某些版本的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文