java - How should a custom Guice scope be integrated with TestNG? -
we use custom guice scope, @testscoped
, of our junit tests lasts single test method, , junit @rule
enter , exit scope appropriately. looks this:
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" @testscoped, we'll have new instance } }
we're starting use testng of our tests in other projects, , we'd have similar pattern. far we've come this:
@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" @testscoped, we'll have new instance } } public class customtestnglistener implements ihookable { @override public void run(ihookcallback callback, itestresult testresult) { testscope.instance.enter(); try { callback.runtestmethod(testresult); } { testscope.instance.exit(); } } }
there couple issues design:
unlike junit, testng uses same instance of test class each method. means have inject
provider<thing>
instead ofthing
, awkward.for reason,
customtestnglistener
running on of our tests, ones don't have@listeners(customtestnglistener.class)
annotation. i've worked around explicitly checking annotation in listener itself, feels hack (though see mockitotestnglistener same thing).
does more familiarity testng have suggestions dealing these issues?
instead of
public class mytestngtest { @inject private provider<thing> thingprovider; @test public void test1() { thing thing = thingprovider.get();
in testng can used
public class mytestngtest { @inject private thing thinginjected; private thing thing; @beforetest public void dobeforetest() { thing = thinginjected.clone(); }
or call thingprovider.get()
in dobeforetest()
, it's better in have lot of @ test
public class mytestngtest { @inject private provider<thing> thingprovider; private thing thing; @beforetest public void dobeforetest() { thing = thingprovider.get(); }
Comments
Post a Comment