[英]Spring Autowired field null on Test Helper Class
Structure.. 结构体..
src/test/java
config
TestConfiguration.java
hooks
WebDriverHooks.java
nicebank
RunSteps.java
OtherSteps..
support
ATMUserInterface.java
KnowsTheDomain.java
@Autowired
is correctly injecting KnowsTheDomain
when placed Steps in nicebank
package. 将Steps放在nicebank
包中时, nicebank
@Autowired
正确注入了KnowsTheDomain
。 But I am unable to @Autowired
KnowsTheDomain
when placed in Helper classes such as WebDriverHooks
and ATMUserInterface
但我无法@Autowired
KnowsTheDomain
放入Helper类,如当WebDriverHooks
和ATMUserInterface
Does it require configuring annotation when autowiring to different packages? 自动布线到其他软件包时是否需要配置注释? I am running Cucumber runner.. 我正在运行黄瓜赛跑者..
From WebDriverHook.java and ATMUserInterface.java, the field private KnowsTheDomain helper;
在WebDriverHook.java和ATMUserInterface.java中,字段private KnowsTheDomain helper;
is returning null instead of singleton instance. 返回null而不是单例实例。 I need them to return what it returns when I run Steps in nicebank
package. 我需要它们返回在nicebank
包中运行“步骤”时返回的内容。
Anyone has idea why this helper
field is null? 任何人都知道为什么此helper
字段为空?
TestConfiguration.java TestConfiguration.java
@Configuration
@ComponentScan(basePackages = { "support"})
public class TestConfiguration {
@Bean
public static KnowsTheDomain knowsTheDomain() {
return new KnowsTheDomain();
}
}
WebDriverHooks.java 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 - this runs the Cucumber runner.. 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 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 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 will work only in beans. @Autowired仅在bean中起作用。
So make sure where are you using @Autowired annotation. 因此,请确保您在哪里使用@Autowired注释。
Add hooks package to @ComponentScan as below 如下所示将钩子包添加到@ComponentScan
@ComponentScan(basePackages = { "support", "hooks" }) @ComponentScan(basePackages = {“ support”,“ hooks”})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.