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.
I just ran a quick test and properties seem to be passed correctly to TestNG:
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/resources/testng-single.yaml</suiteXmlFile>
</suiteXmlFiles>
<systemPropertyVariables>
<foo>bar</foo>
</systemPropertyVariables>
My suite file calls the test class B, which contains:
@Test
public void f() {
System.out.println(" property:" + System.getProperty("foo"));
}
and running it with Maven shows:
property:bar
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.358 sec
I'm not using Guice in this simple example but that's the only difference between your code and mine.
Feel free to create a small Maven project reproducing your problem, make sure that I can compile it and then email it to me.
I tried below test and works fine
My Pom file
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<!--<groups>Regression</groups> -->
<systemPropertyVariables>
<environment>UAT</environment>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
My TestNG test
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestAuthentication {
@Test (groups = { "Sniff", "Regression" })
public void validAuthenticationTest(){
System.out.println(" property:" + System.getProperty("environment"));
}
@Test (groups = { "Regression" },parameters = {"environment"})
public void failedAuthenticationTest(String environment){
System.out.println("Regression-"+environment);
}
@Parameters("environment")
@Test (groups = { "Sniff"})
public void newUserAuthenticationTest(String environment){
System.out.println("Sniff-"+environment);
}
}
来源:https://stackoverflow.com/questions/6280576/how-to-pass-parameters-to-guicified-testng-test-from-surefire-maven-plugin