Unit test and RequestScoped ?

2,411 views
Skip to first unread message

Erland

unread,
Feb 8, 2012, 2:05:12 AM2/8/12
to google-guice
Is there a good way to simulate RequestScoped in a unit test ?

The unit test is not running in a servlet container so when running
the test case I get an error about:
1) No scope is bound to com.google.inject.servlet.RequestScoped.

Ideally I think I'd like RequestScoped to be bound to the scope of the
current test method in the TestNG test case.

Is there a way to do this without implementing my own scope and
remapping it to the RequestScoped when running the test case by
including a module like this in the test case:

public static class RequestScopeModule extends AbstractModule {
@Override
protected void configure() {
bindScope(RequestScoped.class, new MyMethodScope());
}
}

Theoretically it might work if I remap it to Scopes.SINGLETON but I
feel that doing that will risk one test method to affect the outcome
of the next test method.

Any ideas regarding the best way to accomplish this ?

Christian Gruber

unread,
Feb 8, 2012, 8:05:45 AM2/8/12
to google...@googlegroups.com
Well, apart from unit tests that use Guice to wire up dependencies no
being unit tests, I was under the impression that guiceberry was the
ticket for this sort of integration test scenario. Alternately you
can manually register your own request and session scopes in startup
and seed them with fake or mock values.

Regards,
Christian
Sent from my iPhone.

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

Christian Gruber

unread,
Feb 8, 2012, 8:09:59 AM2/8/12
to Christian Gruber, google...@googlegroups.com
Sorry. Hit send too soon. I meant to say create your own request
scope *as you suggested*

It's not a lot of work to do so especially since that scope
infrastructure can be shared among your tests.

Regards,
Christian
Sent from my iPhone.

Sam Berlin

unread,
Feb 8, 2012, 8:43:35 AM2/8/12
to google...@googlegroups.com

I personally find guiceberry far too heavyweight for something as simple as this. Just create a module with the scopes bound and that's all you need.
sam

cowwoc

unread,
Feb 8, 2012, 12:41:17 PM2/8/12
to google...@googlegroups.com
Message has been deleted

Kazimierz Pogoda

unread,
Feb 9, 2012, 3:34:41 PM2/9/12
to google...@googlegroups.com
Here you can find my approach to the problem:

http://code.google.com/p/tadedon/source/browse/tadedon-guice-servlet-mock/src/test/java/com/xemantic/tadedon/guice/servlet/mock/FakeServletContainerTest.java


On Thu, Feb 9, 2012 at 10:48 AM, Thomas Broyer <t.br...@gmail.com> wrote:
> Isn't that what ServletScopes.scopeRequest is for? (never used it though, so
> I can't help using it, sorry)


>
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.

> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-guice/-/XayMlVXlynYJ.


>
> To post to this group, send email to google...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-guice...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.

--
Kazik 'morisil' Pogoda
http://memy.xemantic.com/
http://blog.xemantic.com/

Reply all
Reply to author
Forward
0 new messages