簡體   English   中英

Spring Autowired字段在Test Helper類上為null

[英]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類,如當WebDriverHooksATMUserInterface

自動布線到其他軟件包時是否需要配置注釋? 我正在運行黃瓜賽跑者..

在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM