Java 自定义Guice范围应该如何与TestNG集成?

Java 自定义Guice范围应该如何与TestNG集成?,java,junit,testng,guice,Java,Junit,Testng,Guice,我们使用一个定制的Guice作用域,@TestScoped,用于一些持续一个测试方法的JUnit测试,并使用JUnit@规则适当地进入和退出作用域。看起来是这样的: public class MyJUnitTest { @Rule public CustomRule customRule = new CustomRule(MyModule.class); @Inject private Thing thing; @Test public void test1(

我们使用一个定制的Guice作用域,
@TestScoped
,用于一些持续一个测试方法的JUnit测试,并使用JUnit
@规则
适当地进入和退出作用域。看起来是这样的:

public class MyJUnitTest {
    @Rule public CustomRule customRule = new CustomRule(MyModule.class);

    @Inject private Thing thing;

    @Test
    public void test1() {
        // Use "thing"
    }

    @Test
    public void test2() {
        // Assuming "Thing" is @TestScoped, we'll have a new instance
    }
}
我们开始在其他项目的一些测试中使用TestNG,我们希望有一个类似的模式。到目前为止,我们已经得出以下结论:

@Listeners(CustomTestNGListener.class)
@Guice(modules = MyModule.class)
public class MyTestNGTest {
    @Inject private Provider<Thing> thingProvider;

    @Test
    public void test1() {
        Thing thing = thingProvider.get();
        // Use "thing"
    }

    @Test
    public void test2() {
        Thing thing = thingProvider.get();
        // Assuming "Thing" is @TestScoped, we'll have a new instance
    }
}

public class CustomTestNGListener implements IHookable {
    @Override
    public void run(IHookCallBack callBack, ITestResult testResult) {
        TestScope.INSTANCE.enter();
        try {
            callBack.runTestMethod(testResult);
        } finally {
            TestScope.INSTANCE.exit();
        }
    }
}
@Listeners(CustomTestNGListener.class)
@Guice(modules=MyModule.class)
公共类MyTestNGTest{
@注入私有提供者thingProvider;
@试验
公共void test1(){
Thing=thingProvider.get();
//使用“东西”
}
@试验
公共无效测试2(){
Thing=thingProvider.get();
//假设“Thing”是@testscope,我们将有一个新实例
}
}
公共类CustomTestNGListener实现IHookable{
@凌驾
公共无效运行(IHookCallBack回调、ITestResult testResult){
TestScope.INSTANCE.enter();
试一试{
runTestMethod(testResult);
}最后{
TestScope.INSTANCE.exit();
}
}
}
这种设计有几个问题:

  • 与JUnit不同,TestNG为每个方法使用相同的测试类实例。这意味着我们必须注入
    提供者
    ,而不仅仅是
    东西
    ,这很尴尬

  • 出于某种原因,
    CustomTestNGListener
    正在我们所有的测试上运行,即使那些没有
    @Listeners(CustomTestNGListener.class)
    注释的测试也是如此。我通过在监听器本身中显式地检查注释来解决这个问题,但它感觉像是一个黑客(尽管我确实看到它做了同样的事情)

熟悉TestNG的人对处理这些问题有什么建议吗?

而不是

public class MyTestNGTest {
    @Inject private Provider<Thing> thingProvider;

    @Test
    public void test1() {
        Thing thing = thingProvider.get();
或者只需在
doBeforeTest()
中调用
thingProvider.get()
,这样做更好,因为您有很多
@Test

public class MyTestNGTest {
    @Inject private Provider<Thing> thingProvider;
    private Thing thing;

    @BeforeTest
    public void doBeforeTest() {
        thing = thingProvider.get();
    }
公共类MyTestNGTest{
@注入私有提供者thingProvider;
私事;
@试验前
公开无效的重新测试(){
thing=thingProvider.get();
}

我们几乎没有一种类型是克隆的。至少,您可以在@BeforeTest中调用“thing=thingProvider.get();”,而不是在@Test中调用。如果您有大量的@Test,它就是克隆的better@ViacheslavVedenin我不相信这在并行运行时会起作用。因此,对于许多人来说,这可能不是一个选项。您必须调用get()在测试方法中,我并没有低估你,如果你运行MyTestNGTest 10次,你有10个MyTestNGTest实例,它们分别工作。
public class MyTestNGTest {
    @Inject private Provider<Thing> thingProvider;
    private Thing thing;

    @BeforeTest
    public void doBeforeTest() {
        thing = thingProvider.get();
    }