Guice Constructor Injection with TestNG

501 views
Skip to first unread message

Jeff

unread,
Oct 4, 2011, 6:53:57 PM10/4/11
to testng...@googlegroups.com
Guice "Best Practices" recommends using constructor injection to create "immutable" objects.  This is great until the test class needs resources that are tied to TestNG test threads (unless I'm missing something).
 
If I create a custom Guice Module/Provider to handle injecting MyPerThreadResource, I could use @Test method injection:
@Test
@Inject
public void test1(MyPerThreadResource resource) {
   // use resource
}
Or, similarly @BeforeClass method injection (assumes the class is marked as @Test(singleThreaded=true) ):
@BeforeClass
@Inject
public void beforeClass(MyThreadResource resource) { 
   this.resource = resource;
}
 
I considered Field Injection at the test class level, but I don't think the Test Classes are being instantiated on the same thread used to run them. 
 
Is there a better way to tie resources to the test threads (particularly using DI)? 
 
--
 
Jeff Vincent
See my LinkedIn profile at:
I ♥ DropBox !! 
 

Cédric Beust ♔

unread,
Oct 5, 2011, 2:37:37 AM10/5/11
to testng...@googlegroups.com
Hi Jeff,

I'm not sure I see the problem with injecting in a multithreaded context as long as you pay attention to how you access the injected resource...

But maybe I'm missing something?

-- 
Cédric




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

Jeff

unread,
Oct 5, 2011, 1:38:24 PM10/5/11
to testng...@googlegroups.com
The resource in question is a Selenium2 WebDriver object. 
 
The @Tests in my test class(es) represent a sequence of steps in a workflow/use case that must be executed in a pre-determined order using the same WebDriver object (browser window).  I've set each class to run 'singleThreaded=true' and use the dependsOnMethods to chain the @Test methods (sidenote: is there a less maintenance heavy way to chain dependent test cases --- say alphabetically by name?).
 
In the past, I've used two approaches:
  1. Each class creates it's own WebDriver object, where the class was responsible for the setup/teardown of the driver (which launches a web browser).  This process is fairly expensive to do for each class.
  2. Implement a custom WebDriver factory that would return the driver for the current thread Id (called in @BeforeClass).  This seemed to work okay for the limited time I used it.
I'm looking to learn/try Guice/DI to inject the correct WebDriver object into to each class based on test thread and *only* the test threads (rules out constructor injection?).  When a class runs, I'd like to inject the correct WebDriver based on the test thread (somehow).  This is what I'm still learning/trying to work out.
 
Hopefully that made sense.
 
 
2011/10/5 Cédric Beust ♔ <ced...@beust.com>

Jeff

unread,
Oct 6, 2011, 12:29:36 PM10/6/11
to testng...@googlegroups.com
...not trying to be too big of a pain, but any thoughts/discussion on possible ways to handle this? 
 
One specific question that I still don't know ... if my class is singleThreaded=true, is there a guarantee that the @BeforeClass method will be called on the same thread as all the other @Test methods in the class?
 
If so, I think I just need to inject the resouces in the @BeforeClass method with a custom Guice Module that handles detecting/retrieving the resource based on thread.
 
Has anyone else wrestled with this?  I'm curious how you handle thread-specific resources.

gradeawarrior

unread,
Apr 3, 2012, 10:01:01 PM4/3/12
to testng...@googlegroups.com

This is also something I'm interested in. Was an answer ever found for this?


Jeff-351 wrote:
>
> ...not trying to be too big of a pain, but any thoughts/discussion on
> possible ways to handle this?
>
> One specific question that I still don't know ... if my class is
> singleThreaded=true, is there a guarantee that the @BeforeClass method
> will
> be called on the same thread as all the other @Test methods in the class?
>
> If so, I think I just need to inject the resouces in the @BeforeClass
> method
> with a custom Guice Module that handles detecting/retrieving the resource
> based on thread.
>
> Has anyone else wrestled with this? I'm curious how you handle
> thread-specific resources.
> On Wed, Oct 5, 2011 at 11:38 AM, Jeff <preda...@gmail.com> wrote:
>
>> The resource in question is a Selenium2 WebDriver object.
>>
>> The @Tests in my test class(es) represent a sequence of steps in a
>> workflow/use case that must be executed in a pre-determined order using
>> the
>> same WebDriver object (browser window). I've set each class to run
>> 'singleThreaded=true' and use the dependsOnMethods to chain the @Test

>> methods *(sidenote: is there a less maintenance heavy way to chain
>> dependent test cases --- say alphabetically by name?)*.


>>
>> In the past, I've used two approaches:
>>

>> 1. Each class creates it's own WebDriver object, where the class was


>> responsible for the setup/teardown of the driver (which launches a web
>> browser). This process is fairly expensive to do for each class.

>> 2. Implement a custom WebDriver factory that would return the driver

>>>> I ♥ DropBox <http://db.tt/9O6LfBX> !!


>>>>
>>>>
>>>> --
>>>> 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.
>>>>
>>>
>>> --
>>> 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.
>>>
>>
>>
>>
>> --
>> Jeff Vincent
>> preda...@gmail.com
>> See my LinkedIn profile at:
>> http://www.linkedin.com/in/rjeffreyvincent

>> I ♥ DropBox <http://db.tt/9O6LfBX> !!


>>
>>
>
>
> --
> Jeff Vincent
> preda...@gmail.com
> See my LinkedIn profile at:
> http://www.linkedin.com/in/rjeffreyvincent

> I ♥ DropBox <http://db.tt/9O6LfBX> !!


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

--
View this message in context: http://old.nabble.com/Guice-Constructor-Injection-with-TestNG-tp32593158p33545044.html
Sent from the testng-users mailing list archive at Nabble.com.

Reply all
Reply to author
Forward
0 new messages