问题
Let's say I have some guice module/provider which supposed to create bindings based on parameters received from TestNg suite file. e.g.
<test name="Test">
<parameter name="profile" value="chrome"></parameter>
<classes>
<class name="com.apc.ui.tests.TestClass">
</class>
</classes>
</test>
What I wanted to achieve is a possibility to access parameter value from withing above mentioned module. e.g.
public class MyModule extends AbstractModule {
@Inject
ITestContext context;
@Override
protected void configure() {
...
}
}
So, I'm wondering whether it's possible. Any alternatives are also really welcomed. Thanks.
回答1:
Eventually, did manage to find a solution in testNg sources. There is a possibility to set a so called parent module in a suite file.
<suite name="Suite1" verbose="1" parallel="false"
parent-module="org.my.tests.ParentModule">
...
the module can receive ITestContext as a constructor parameter which means it can be then injected to other classes:
public class ParentModule extends AbstractModule {
private ITestContext context;
public GuiceParentModule(ITestContext context) {
this.context = context;
}
@Override
protected void configure() {
bind(ITestContext.class).toInstance(context);
}
...
来源:https://stackoverflow.com/questions/50351001/is-there-a-way-to-inject-itestcontext-from-testng-to-guice-module