I am trying to create two custom scopes for JUnit: one that is test-
scoped (per @Test method) and one that is testclass-scoped (per Test
class), both thread safe. This way I can create different Guice
modules that bind to specific scopes. So far so good.

For the most part I got a simple implementation working quite well and
I am managing the lifecycle of instances by hooking into JUnit's
lifecycle calls to @BeforeClass, @AfterClass, @Before, @After. The
problem with this approach is that login() and logout() are called
even if I never used the instance in my tests. This is not really a
big problem since in most cases I will use them. However, I am curious
about how to make this a bit more lazy: i.e. login and logout only if
the instance is ever used in my tests.

One approach could be to add an entry()/exit() to the Scope, called
from the JUnit Before/After, and have the Scope automatically dispose
the instances on exit(). For 3rd party classes I wont be able to
implement Disposable so I would have to wrap it in another class.
However, it seems that Guice already have a factory wrapper for
classes: it's called Provider<T>. Wouldn't it make sense to have a
Provider<T>.unget() (the dual of get())? What I like about this
approach is that it would be possible to have custom login()/logout()
logic in a single place and, if this were part of Guice's framework,
then a Scope would be able to automatically call unget() to dispose of
an instance.

Or maybe there is another, better way of dealing with this that I am
not aware of?

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

Reply via email to