[英]Spring Autowired field null on Test Helper Class
結構體..
src/test/java
config
TestConfiguration.java
hooks
WebDriverHooks.java
nicebank
RunSteps.java
OtherSteps..
support
ATMUserInterface.java
KnowsTheDomain.java
將Steps放在nicebank
包中時, nicebank
@Autowired
正確注入了KnowsTheDomain
。 但我無法@Autowired
KnowsTheDomain
放入Helper類,如當WebDriverHooks
和ATMUserInterface
自動布線到其他軟件包時是否需要配置注釋? 我正在運行黃瓜賽跑者..
在WebDriverHook.java和ATMUserInterface.java中,字段private KnowsTheDomain helper;
返回null而不是單例實例。 我需要它們返回在nicebank
包中運行“步驟”時返回的內容。
任何人都知道為什么此helper
字段為空?
TestConfiguration.java
@Configuration
@ComponentScan(basePackages = { "support"})
public class TestConfiguration {
@Bean
public static KnowsTheDomain knowsTheDomain() {
return new KnowsTheDomain();
}
}
WebDriverHooks.java
@ContextConfiguration(classes = TestConfiguration.class, loader=AnnotationConfigContextLoader.class)
@Configurable(autowire = Autowire.BY_TYPE)
public class WebDriverHooks {
@Autowired
private KnowsTheDomain helper;
@After
public void finish(Scenario scenario) {
try {
byte[] screenshot =
helper.getWebDriver().getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
} catch (WebDriverException somePlatformsDontSupportScreenshots) {
System.err.println(somePlatformsDontSupportScreenshots.getMessage());
}
finally {
helper.getWebDriver().close();
}
}
}
RunSteps.java-這將運行Cucumber運行程序。
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty", "html:out"},
snippets = SnippetType.CAMELCASE,
features = "classpath:cucumber",
dryRun = false)
@ContextConfiguration(classes = TestConfiguration.class)
public class RunSteps {
}
KnowsTheDomain.java
@Component
public class KnowsTheDomain {
private Account myAccount;
private CashSlot cashSlot;
private Teller teller;
private EventFiringWebDriver webDriver;
public Account getMyAccount() {
if (myAccount == null) {
myAccount = new Account();
}
return myAccount;
}
public CashSlot getCashSlot() {
if (cashSlot == null) {
cashSlot = new CashSlot();
}
return cashSlot;
}
public Teller getTeller() {
if (teller == null) {
teller = new ATMUserInterface();
}
return teller;
}
public EventFiringWebDriver getWebDriver() {
if (webDriver == null) {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver_win32/chromedriver.exe");
webDriver = new EventFiringWebDriver(new ChromeDriver());
}
return webDriver;
}
}
ATMUserInterface.java
@ContextConfiguration(classes = TestConfiguration.class, loader=AnnotationConfigContextLoader.class)
@Configurable(autowire = Autowire.BY_TYPE)
public class ATMUserInterface implements Teller {
@Autowired
private KnowsTheDomain helper;
@Override
public void withdrawFrom(Account account, int dollars) {
try {
helper.getWebDriver().get("http://localhost:" + ServerHooks.PORT);
helper.getWebDriver().findElement(By.id("Amount"))
.sendKeys(String.valueOf(dollars));
helper.getWebDriver().findElement(By.id("Withdraw")).click();
} catch (Exception e) {
System.err.println("err" + e);
}
}
}
@Autowired僅在bean中起作用。
因此,請確保您在哪里使用@Autowired注釋。
如下所示將鈎子包添加到@ComponentScan
@ComponentScan(basePackages = {“ support”,“ hooks”})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.