2017-05-08 82 views
0

在我的java春天mvc中,我試圖用黃瓜和硒進行測試。 我有以下類,它負責調用驅動程序。硒:依賴注射不起作用

@Service 
public class BaseUtil { 

    public void caller(){ 
     System.out.println("Caller Firefox driver here!!!!!!!"); 
    } 

} 

,然後在步驟定義類我要去,使依賴注入步驟:

@Component 
public class StepDefenitions{ 

    @Autowired 
    BaseUtil base; 


    @Given("^I want to register into the main forum$") 
    public void i_want_to_register_into_the_main_forum() throws Throwable { 
     base.caller(); 

    }} 

但它與

java.lang.NullPointerException 
     at com.exercise.StepDefenitions.i_want_to_register_into_the_main_forum(StepDefenitions.java:26) 

那麼,爲什麼@Autowierd不抱怨在這裏工作?

+0

「StepDefenitions」是一個彈簧管理bean嗎? – Jens

+0

延斯是對的。您是否在StepDefinitions類中擁有@Component或@ Service類 – pvpkiran

+0

否它包含黃瓜功能文件的步驟。但是,我不知道如果我回答你的問題@Jens – Salman

回答

0

StepDefenitions需要成爲託管bean才能注入其依賴關係。

使用@Component或@Service對其進行註釋。

+0

我用我們的解決方案更新了我的問題。我試過'@ Service'和'@ Component'兩個都失敗了 – Salman

+0

您是使用@Configuration還是xml來定義bean? – Raphael

+0

我沒有他們現在。所以,我想,我可以擁有他們兩個 – Salman