I'm using Maven + Surefire + TestNG + Guice (latest stable ones)
I have "large" test that requires Guice to run. Basically I'm doing it this way:
@Test(groups = "large")
@Guice(modules = FooLargeTest.Module.class)
public class FooLargeTest {
public static class Module extends AbstractModule {
public void configure() {
bindConstant().annotatedWith(FooPort.class).to(5000);
// ... some other test bindings
}
}
@Inject Provider<Foo> fooProvider;
@Test
public void testFoo() {
Foo foo = fooProvider.get() // here injection of port is done
// it could not be passed to constructor
// ... actual test of foo
}
}
The problem is that FooPort
is hardcoded to 5000
. It is a Maven property, so the first try was to use next Surefire configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<systemPropertyVariables>
<fooPort>${foo.port}</fooPort>
</systemPropertyVariables>
</configuration>
</plugin>
And after that request it like System.getProperty("fooPort")
. Unfortunately, documentation says, that this is only for JUnit test. At least I could not see this system variable during debugging of a test. I tried forkMode
both default one and never
, it does not change anything. For TestNG tests it's recommended to make it this way:
<properties>
<property>
<name>fooPort</name>
<value>${foo.port}</value>
</property>
</properties>
But now I should use this property from Guice, so it should be given somehow to GuiceModule, I've tried it next way:
@Test(groups = "large")
@Guice(moduleFactory = FooLargeTest.ModuleFactory.class)
public class FooLargeTest {
public static class ModuleFactory extends AbstractModule {
private final String fooPort = fooPort;
@Parameters("fooPort")
public ModuleFactory(String fooPort) {
this.fooPort = fooPort;
}
public Module createModule(final ITestContext context, Class<?> testClass) {
return new AbstractModule {
public void configure() {
bindConstant().annotatedWith(FooPort.class).to(fooPort);
// ... some other test bindings
}
}
}
}
@Inject Provider<Foo> fooProvider;
@Test
public void testFoo() {
Foo foo = fooProvider.get() // here injection of port is done
// actual test of foo
}
}
But this way was also a failure as creator for modulefactories
does not take @Parameters
into account and thus could not create instance of a factory.
Looks like I should try to get some data from ITestContext context
, but I do not know how and if the data is there or if there is some simpler way to do what I want.
Thanks for a response.