我正在努力與黃瓜和春季配置。 我正在使用頁面對象模式和BrowserFactory編寫硒框架。黃瓜春季和班級配置
當我使用@ComponentScan,@Component和@Autowire註釋一切正常,但是當我想在@Configuration類中使用@Bean註釋(BrowserFactory註冊少量瀏覽器驅動程序)創建更復雜的bean時,它不會工作,在調試過程中,我正在嘗試使用Autowire的每個變量的空值。
我使用的是Spring 4.2.4,版本1.2.4中的所有黃瓜依賴項。
配置:
@Configuration
public class AppConfig {
@Bean
@Scope("cucumber-glue")
public BrowserFactory browserFactory() {
BrowserFactory browserFactory = new BrowserFactory();
browserFactory.registerBrowser(new ChromeBrowser());
browserFactory.registerBrowser(new FirefoxBrowser());
return browserFactory;
}
@Bean(name = "loginPage")
@Scope("cucumber-glue")
public LoginPage loginPage() throws Exception {
return new LoginPage();
}
@Bean(name = "login")
@Scope("cucumber-glue")
public Login login() {
return new Login();
}
}
POP:
public class LoginPage extends Page {
public LoginPage() throws Exception {
super();
}
...
}
頁:
public class Page {
@Autowired
private BrowserFactory browserFactory;
public Page() throws Exception{
...
}
}
登錄:
public class Login {
@Autowired
private LoginPage loginPage;
public Login(){}
...
}
個步驟:
@ContextConfiguration(classes = {AppConfig.class})
public class LoginSteps {
@Autowired
Login login;
public LoginSteps(){
}
@Given("^an? (admin|user) logs in$")
public void adminLogsIn(Login.User user) throws Exception {
World.currentScenario().write("Logging in as " + user + "\n");
login.as(user);
}
}
錯誤:
cucumber.runtime.CucumberException: Error creating bean with name 'LoginSteps': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: Login LoginSteps.login; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'login': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private LoginPage Login.loginPage; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loginPage' defined in AppConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [LoginPage]: Factory method 'loginPage' threw exception; nested exception is java.lang.NullPointerException
現在最有趣的部分... BrowserFactory在世界級的是正確的自動裝配Autowired!
世界:
public class World {
@Autowired
private BrowserFactory browserFactory;
...
}