我已经创建了黄瓜测试项目,并成功地通过黄瓜-guice配置了Guice支持。当我添加 serenity-cucmber 并使用 CucumberWithSerenity 配置测试运行程序时,我遇到了一个错误:
More than one Cucumber ObjectFactory was found in the classpath
You probably may have included, for instance, cucumber-spring AND cucumber-guice as part of
your dependencies. When this happens, Cucumber falls back to instantiating the
DefaultJavaObjectFactory implementation which doesn't provide IoC.
In order to enjoy IoC features, please remove the unnecessary dependencies from your class path.
原因是有两个 ObjectFactory 实现:
cucumber.runtime.java.guice.impl.GuiceFactory (in cucumber-guice)
cucumber.runtime.SerenityObjectFactory (in serenity-cucumber)
和黄瓜一起使用?
根据 Serenity-Cucumber 开发者,我们不能使用 Guice,应该使用 Spring。 https://github.com/serenity-bdd/serenity-cucumber/issues/8
在src/test/resources/cucumber.properties
中添加以下属性来选择ObjectFactory
实现:
cucumber.api.java.ObjectFactory = cucumber.runtime.java.guice.impl.GuiceFactory
来源: https://cucumber.io/blog/2015/07/08/polymorphic-step-definitions
请注意,您将失去 Senerity Page
功能。否则,您可以编写自己的ObjectFactory
类来实现GuiceFactory
和SerenityObjectFactory
的功能。