0

我可以像这样注入类字段:

class TestClass{
    @Inject
     Handler handler;

    @Test
    public void test1(){....}

    @Test
    public void test2(){....}
}

但是字段“handler”将在 test1() 和 test2() 之间共享,给我带来很多问题,所以问题是如何为 test1() 和 test2() 注入单独/不同的处理程序,如下所示:

 class TestClass{

  @Test@Inject
  public void test1(Handler handler){....}

  @Test@Inject
    public void test2(Handler handler){....}
 }   
4

1 回答 1

0

TestNG is doing the invocation so you can't add parameters like this or it won't know how to call your test methods. You will need to rely on your injection framework for this (I'm assuming Guice). You can either make sure your handler is not a singleton and define two different fields:

@Inject
private Handler handler1;

@Inject
private Handler handler2;

Or you could use annotated injection if you want specific instances:

@Inject
@Handler1
private Handler handler1;

@Inject
@Handler2
private Handler handler2;
于 2013-06-14T07:00:13.513 回答