我正在使用Maven + Surefire + TestNG + Guice(最新的穩定版本)如何將參數傳遞給Surefire Maven插件的guanted TestNG測試?
我有需要Guice運行的「大型」測試。 基本上我做這種方式:
@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
}
}
的問題是,FooPort
被硬編碼到5000
。這是一個Maven的屬性,因此在第一次嘗試是使用下一個神火配置:
<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>
這要求它像System.getProperty("fooPort")
後。不幸的是,文檔說,這隻適用於JUnit測試。至少我在調試測試期間看不到這個系統變量。我試過forkMode
默認的一個和never
,它不會改變任何東西。對於TestNG進行它的建議做出這樣說:
<properties>
<property>
<name>fooPort</name>
<value>${foo.port}</value>
</property>
</properties>
但是現在我應該從吉斯使用此屬性,所以應該以某種方式給予GuiceModule,我已經試過了下一方式:
@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
}
}
但是這種方式也是失敗的,因爲modulefactories
的創建者不考慮@Parameters
,因此無法創建工廠實例。
看起來我應該嘗試從ITestContext context
獲取一些數據,但我不知道數據是如何存在的,或者是否有一些簡單的方法來實現我想要的。
感謝您的回覆。
貌似我能複製我的問題。這裏是縮小的項目。使用'mvn test'和'mvn -X test'運行項目的結果會被附上。 [附上郵編](http://narod.ru/disk/15469163001/maven-testng.zip.html) – 2011-06-09 07:06:13