Integrating Guice with TestNG listeners

399 views
Skip to first unread message

Karthik Krishnan

unread,
Dec 9, 2009, 7:12:10 PM12/9/09
to testng...@googlegroups.com
Hi

I am using TestNG 5.10 for unit testing and Guice 2.0 for unit testing. I have been able to hook up Guice with TestNG using IObjectFactory and @ObjectFactory annotation and it is working well. I have a question about scopes and listeners.

I used the custom scope implementation from Guice's user guide to generate thread local instances to be injected (Here is the link ; http://code.google.com/docreader/#p=google-guice&s=google-guice&t=CustomScopes) . The document states that to enter and exit the scope, the code should reside in a filter or an interceptor. I am implementing TestNG invoked method listener to enter and exit scopes. However, for the code to work, I need to inject the scope in my listener to start the seed and exit process.

This is what I am think may work. Please correct me if I am wrong. If I can get a handle to the ITestContext, i can get an reference to Guice injector stored as a context attibute. I can use the Guice injector to inject members into listener. I have looked through the code and can not figure out how I can get a handle to the reference. Can any one help?

Thanks,

Kartik

Cédric Beust ♔

unread,
Dec 9, 2009, 7:34:09 PM12/9/09
to testng...@googlegroups.com
You can get the ITestContext in a @BeforeSuite method and store the injector in the attribute.  You can then retrieve this injector in the listeners through that same ITestContext.

--
Cedric


--

You received this message because you are subscribed to the Google Groups "testng-users" group.
To post to this group, send email to testng...@googlegroups.com.
To unsubscribe from this group, send email to testng-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/testng-users?hl=en.



--
Cédric


Karthik Krishnan

unread,
Dec 9, 2009, 8:28:38 PM12/9/09
to testng...@googlegroups.com
Hi Cedric,

Thank you for replying but I can't figure out how to get  I need to implement IInvokedMethodlListener to get to implement before and after behavior. I can not figure out how I can access ITestContext from IInvokedMethod or ITestResult. IInvokedMethod#getTestMethod() returns ITestMethod instance. How do I get a handle to ITestContext.

Thanks,

Kartik

2009/12/9 Cédric Beust ♔ <cbe...@google.com>

Karthik Krishnan

unread,
Dec 10, 2009, 2:59:47 PM12/10/09
to testng...@googlegroups.com
Is there a way to get a handle to ITestContext from either IInvokedMethod or ITestResult?

Cédric Beust ♔

unread,
Dec 10, 2009, 3:54:01 PM12/10/09
to testng...@googlegroups.com
It's not possible at the moment, I'm considering introducing IInvokedMethodListener2 that would be just like IInvokedMethodListener but that would pass an ITestContext to the listener methods...

-- 
Cedric

Karthik Krishnan

unread,
Dec 11, 2009, 5:44:57 PM12/11/09
to testng...@googlegroups.com
Hi Cedric,

Is there a work around?

 If you want, I can also take a look at defining and its invocation by the TestNGRunner/SuiteRunner. If you can point me to where I should look for, I will take a look at it?

Thanks,

Kartik

2009/12/10 Cédric Beust ♔ <cbe...@google.com>
Reply all
Reply to author
Forward
0 new messages