2015-03-03 90 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公众访问。

相关问题