Maven + Surefire + TestNG + Guice (最新の安定版) を使用しています
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 プロパティであるため、最初の試みは次の Surefire 構成を使用することでした。
<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>
しかし、今は Guice からこのプロパティを使用する必要があるため、何らかの方法で 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
ますが、データがそこにあるかどうか、または必要なことを行うためのより簡単な方法があるかどうかはわかりません。
返信ありがとうございます。