2015-03-03 27 views
1
public class AbstractTest implements ITestListener { 

    @Inject 
    protected MobConfiguration mob; 

    @Override 
    public void onStart(ITestContext context) { 
     // TODO Auto-generated method stub 
    } 
} 

當我試圖在listener類中注入依賴關係時,它總是返回null?在偵聽器或攔截器實現者類中是否有任何可能的方法來處理DI?是否有可能將依賴關係綁定到TestNG中的攔截器?

+0

_你是否試圖注入依賴關係? – 2015-03-03 06:26:16

+0

binder.bind(xxx.class).to(yyy.class); – 2015-03-10 05:41:36

回答

1

我已成功嘗試this approach(使用工廠,此答案結尾的代碼示例)。還有一點需要注意,如果您使用組註釋,則區分(如TestDIFactory.java代碼中的第126行 - there again)似乎只有在使用testng.xml文件時纔可靠。

否則使用註釋參數僅默認情況下(l. 130, TestDIFactory.java)似乎變得活躍。人們可以很容易檢查,通過後l. 122, TestDIFactory.java

加入if(context.getIncludedGroups().length == 0) throw new NullPointerException("no groups found");如果需要implements ITestListener明確它應該很容易修改相應的public void onStart(ITestContext context)方法。

@Guice(moduleFactory = TestDIFactory.class) 
public class YourTestClass { 
@Inject protected MobConfiguration mob; 

@Test(groups = {"unit"}) 
public void yourtest() {} 
} 

編輯: 我已經證明工廠方法有問題的一種情況下: 如果由工廠份額綁定意味着在模塊A結合的一個對象提供的模塊也被在另一個模塊B結合的,但你要求的組合在A和B上安裝/調用配置的模塊,然後由工廠返回。遇到InstantiationException秒的高變化。所以我的經驗法則是:工廠只在每次測試只需要一個模塊。在其他情況下,我使用我。即@Guice(modules = {TestDIFactory.A.class, TestDIFactory.B.class})雖然假設給予A和B公衆訪問。

相關問題