Maven 2 如何从Surefire Maven插件向guicified TestNG测试传递参数?

Maven 2 如何从Surefire Maven插件向guicified TestNG测试传递参数?,maven-2,testng,guice,maven-surefire-plugin,Maven 2,Testng,Guice,Maven Surefire Plugin,我正在使用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

我正在使用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
  }
}
<properties>
  <property>
    <name>fooPort</name>
    <value>${foo.port}</value>
  </property>
</properties>
在这个请求之后,它像
System.getProperty(“fooPort”)
一样。不幸的是,文档中说,这只适用于JUnit测试。至少在调试测试期间,我看不到这个系统变量。我尝试了
forkMode
默认模式和
never
,它没有改变任何东西。对于TestNG测试,建议这样做:

@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
  }
}
<properties>
  <property>
    <name>fooPort</name>
    <value>${foo.port}</value>
  </property>
</properties>

福波特
${foo.port}
但是现在我应该从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
  }
}
@Test(groups=“large”)
@Guice(moduleFactory=test.moduleFactory.class)
公开课考试{
公共静态类ModuleFactory扩展了AbstractModule{
私有最终字符串fooPort=fooPort;
@参数(“fooPort”)
公共ModuleFactory(字符串端口){
this.fooPort=fooPort;
}
公共模块createModule(最终ITestContext上下文,类testClass){
返回新的抽象模块{
public void configure(){
bindConstant().annotatedWith(FooPort.class).to(FooPort);
//…其他一些测试绑定
}
}
}
}
@注入提供者,注入提供者;
@试验
公共void testFoo(){
Foo-Foo=fooProvider.get()//此处完成端口注入
//foo的实际测试
}
}
但这种方法也是失败的,因为
模块化因子
的创建者没有考虑
@参数
,因此无法创建工厂实例

看起来我应该尝试从
ITestContext
中获取一些数据,但我不知道如何以及数据是否存在,或者是否有更简单的方法来实现我的目标


感谢您的回复。

我刚刚运行了一个快速测试,属性似乎正确地传递给了TestNG:

    <configuration>
      <suiteXmlFiles>
        <suiteXmlFile>src/test/resources/testng-single.yaml</suiteXmlFile>
      </suiteXmlFiles>
      <systemPropertyVariables>
        <foo>bar</foo>
      </systemPropertyVariables>
用Maven运行它会显示:

property:bar
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.358 sec
在这个简单的示例中,我没有使用Guice,但这是您的代码和我的代码之间的唯一区别


请随意创建一个小型Maven项目来重现您的问题,确保我可以编译它,然后通过电子邮件发送给我。

我刚刚运行了一个快速测试,属性似乎正确地传递给了TestNG:

    <configuration>
      <suiteXmlFiles>
        <suiteXmlFile>src/test/resources/testng-single.yaml</suiteXmlFile>
      </suiteXmlFiles>
      <systemPropertyVariables>
        <foo>bar</foo>
      </systemPropertyVariables>
用Maven运行它会显示:

property:bar
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.358 sec
在这个简单的示例中,我没有使用Guice,但这是您的代码和我的代码之间的唯一区别


请随意创建一个小的Maven项目来重现您的问题,确保我可以编译它,然后通过电子邮件发送给我。

我尝试了下面的测试,效果很好

我的Pom文件

<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>

我试过低于测试,效果很好

我的Pom文件

<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>

看来我可以重现我的问题了。这是缩小的项目。附上了使用
mvn测试
mvn-X测试
运行项目的结果。看来我可以重现我的问题了。这是缩小的项目。附上了使用
mvn测试
mvn-X测试
运行项目的结果。