我正在尝试使用picocontainer进行DI,但仍然会将我的共享对象实例化多次,而不是自动作为单例进行管理。这是一个例子来说明。类ASteps和BSteps通过其构造函数接收SharedObject实例。我希望它可以通过picocontainer作为单例进行管理:根据Cucumber文档仅实例化一次。相反,我看到它为ASteps实例化一次,为BSteps实例化一次:
Running my.domain.CucumberRunTest
INFO [main] (CucumberHooks.java:15) - Executing before()
INFO [main] (SharedObject.java:11) - SharedObject - instantiated
INFO [main] (ASteps.java:21) - Executing a_step_one()
INFO [main] (ASteps.java:26) - Executing a_step_two()
INFO [main] (ASteps.java:31) - Executing a_step_three()
INFO [main] (CucumberHooks.java:20) - Executing after()
INFO [main] (CucumberHooks.java:15) - Executing before()
INFO [main] (SharedObject.java:11) - SharedObject - instantiated
INFO [main] (BSteps.java:23) - Executing b_step_one()
INFO [main] (BSteps.java:28) - Executing b_step_two()
INFO [main] (BSteps.java:33) - Executing b_step_three()
INFO [main] (CucumberHooks.java:20) - Executing after()
我做错了什么?这是代码:
package my.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class ASteps {
final Logger log = LoggerFactory.getLogger(getClass());
SharedObject sharedObject;
public ASteps(SharedObject sharedObject) {
this.sharedObject = sharedObject;
}
@Given("^A step one$")
public void a_step_one() {
log.info("Executing a_step_one()");
}
@When("^A step two$")
public void a_step_two() {
log.info("Executing a_step_two()");
}
@Then("^A step three$")
public void a_step_three() {
log.info("Executing a_step_three()");
}
}
************************
package my.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class BSteps {
final Logger log = LoggerFactory.getLogger(getClass());
SharedObject sharedObject;
public BSteps(SharedObject sharedObject) {
this.sharedObject = sharedObject;
}
@Given("^B step one$")
public void b_step_one() {
log.info("Executing b_step_one()");
}
@When("^B step two$")
public void b_step_two() {
log.info("Executing b_step_two()");
}
@Then("^B step three$")
public void b_step_three() {
log.info("Executing b_step_three()");
}
}
************************
package my.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cucumber.api.java.After;
import cucumber.api.java.Before;
public class CucumberHooks {
final Logger log = LoggerFactory.getLogger(getClass());
@Before
public void before() {
log.info("Executing before()");
}
@After
public void after() {
log.info("Executing after()");
}
}
*********************
package my.domain;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
public class CucumberRunTest {
}
**********************
package my.domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SharedObject {
final Logger log = LoggerFactory.getLogger(getClass());
public SharedObject() {
log.info("SharedObject - instantiated");
}
}
答案 0 :(得分:2)
当场景结束时处理世界时,pico容器的缓存在场景之间为reset。
这非常每个设计以避免场景之间的状态泄漏并使它们隔离,因此测试的顺序不会影响结果。
如果您确实希望在方案A和方案B之间保持状态,则需要在pico容器之外自己处理SharedObject
,或者您可以明确地将这两种方案之间的依赖关系 - 例如通过使用Background。
答案 1 :(得分:1)
我希望现在回答你的问题还为时不晚。
实际上,你可以拥有你想要的东西。 我看到你也想拥有一个浏览器实例。 您必须使用静态对象才能执行此操作。 我创建了一个小类来避免每个测试之间的驱动程序的打开/关闭。它由picocontainer管理。在您的stepDefinition类中,您必须实现"相同的"构造
以下是一个示例:
public class myStepDefinition{
private Drivers context;
public myStepDefinition(Drivers context){
this.context = context;
// whatever you want to do
}
}
public class Drivers {
private static boolean initialized = false;
private static WebDriver driver;
@Before
public void initialize(){
if (!initialized){
initialized = true;
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.myurl.url");
}
}
public static WebDriver getDriver(){
return driver;
}
}
请记住,您必须在每次测试(通过或失败)后返回登录页面或起始页面
此致
尼古拉斯