How to effectively use PicoContainer Setter Injection (Use PicoContainer without passing parameters to constructor)?
How to effectively use PicoContainer Setter Injection (Use PicoContainer without passing parameters to the constructor)?
如何有效使用PicoContainerSetter注入?
我需要能够跨 Cucumber 步骤传递数据,并且希望避免将参数传递给构造函数的传统 PicoContainer 方法(使用 PicoContainer 而不采用将 classes 注入其他 classes 通过构造函数)。
到目前为止,我有以下设置但没有成功,有什么想法吗?
我的步数class:
public class MainSteps extends Base {
DefaultPicoContainer pico = new DefaultPicoContainer(new SetterInjection("init"));
public MainSteps() {
this.pico.addComponent(Account_Pojo.class);
}
@Given("^I went to the farmm$")
public void i_went_to_the_farm() {
loadUrl(Global_Vars.HOMEPAGE_URL);
Account_Pojo account_pojo = pico.getComponent(Account_Pojo.class);
account_pojo.setFirstName("joe");
System.out.println(account_pojo.getFirstName()); //outputs joe
}
@Given("^I went to the zoo$")
public void i_went_to_the_zoo() {
Account_Pojo account_pojo = pico.getComponent(Account_Pojo.class);
System.out.println(account_pojo.getFirstName()); //null pointer exception
}
}
Pojo 设置:
public class Account_Pojo {
private String firstName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
}
正如您从上面的代码中看到的,位于第二步中的以下代码行应该 return joe 但是它似乎 returning 一个空值:
System.out.println(account_pojo.getFirstName()); //null pointer exception
将 firstName 更改为 static like
private static String firstName;
或
将相同的对象reference
传递给你
System.out.println(account_pojo.getFirstName());
如何有效使用PicoContainerSetter注入?
我需要能够跨 Cucumber 步骤传递数据,并且希望避免将参数传递给构造函数的传统 PicoContainer 方法(使用 PicoContainer 而不采用将 classes 注入其他 classes 通过构造函数)。
到目前为止,我有以下设置但没有成功,有什么想法吗?
我的步数class:
public class MainSteps extends Base {
DefaultPicoContainer pico = new DefaultPicoContainer(new SetterInjection("init"));
public MainSteps() {
this.pico.addComponent(Account_Pojo.class);
}
@Given("^I went to the farmm$")
public void i_went_to_the_farm() {
loadUrl(Global_Vars.HOMEPAGE_URL);
Account_Pojo account_pojo = pico.getComponent(Account_Pojo.class);
account_pojo.setFirstName("joe");
System.out.println(account_pojo.getFirstName()); //outputs joe
}
@Given("^I went to the zoo$")
public void i_went_to_the_zoo() {
Account_Pojo account_pojo = pico.getComponent(Account_Pojo.class);
System.out.println(account_pojo.getFirstName()); //null pointer exception
}
}
Pojo 设置:
public class Account_Pojo {
private String firstName;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
}
正如您从上面的代码中看到的,位于第二步中的以下代码行应该 return joe 但是它似乎 returning 一个空值:
System.out.println(account_pojo.getFirstName()); //null pointer exception
将 firstName 更改为 static like
private static String firstName;
或
将相同的对象reference
传递给你
System.out.println(account_pojo.getFirstName());