I am writing a webDriver end to end test framework using junit 4 and guice for injection.
I want to pass the test name into my driverFactory so that I can use it to name the test properly in Saucelabs:
in my AbstractTest (used by currently all tests) I use
@Rule
public TestName name = new TestName();
and
@Before
public void before() {
System.setProperty("testName", name.getMethodName());
// This is unfortunately, the only way I have managed to make the test name available to the configuration factory
Guice.createInjector(new TestModule()).injectMembers(this);
I can then retrieve this value from inside my TestModule constructor using
testConfig.setTestName(System.getProperty("testName"));
I tried many other approaches to make my testName available to the TestModule where I can make it available to other classes. The all fall over for basically the same reason:
I can set a field in the test module created here, but the first time that a TestModule is injected, it is second new instance and it is this second instance that is now injected without the correct field value.
I would prefer another strategy than setting a System property but I failed hopelessly. Can anyone suggest anything better?
(If its relevant I am running my tests in parallel from Gradle)
You can use BoundFieldModule
for this:
@Rule @Bind
public final TestName name = new TestName();
@Before
public void injectMembers() {
Guice.createInjector(
BoundFieldModule.of(this);
new TestModule())
.injectMembers(this);
}