how to test session/request scopes ?

734 views
Skip to first unread message

naaka

unread,
Jun 12, 2009, 12:18:46 PM6/12/09
to google-guice
hi,
i tried out google guice and it looks great.

i have tdd-ed a small spike with persistence and singleton/no scopes
and every worked fine.

now i have added the servlet-guice extension, and changed a class to
session scope, but my test complaint:

com.google.inject.ProvisionException: Guice provision errors:

1) Error in custom provider, com.google.inject.OutOfScopeException:
Cannot access scoped object. Either we are not currently inside an
HTTP Servlet request, or you may have forgotten to apply
com.google.inject.servlet.GuiceFilter as a servlet filter for this
request.
while locating com.naaka.spikes.domain.User
for field at com.naaka.spikes.domain.UserTest.defaultUser
(UserTest.java:24)
while locating com.naaka.spikes.domain.UserTest


i already added the servlet module in the setup method:

@BeforeClass
public void setupInjection() {
...
modules.add(declarePersistence);
modules.add(configPersistence);
modules.add(new ServletModule());
injector = Guice.createInjector(modules);
...
}

how i can setup a test to work with session modules ?

do i have to mock something (servlet request/response/filter) ?

is there a base test class or a test i can copy from ?



THANKS !

Dhanji R. Prasanna

unread,
Jun 12, 2009, 12:32:19 PM6/12/09
to google...@googlegroups.com
You should bind @RequestScoped and @SessionScoped to your own mock Scope implementation. Then Guice will use this instead of its default HTTP scopes. In this case you want to omit the ServletModule in your test.

MockRequestScope myMockScope = new MockRequestScope();
bindScope(RequestScoped.class, myMockScope);

//after tests
myMockScope.assertThatThingsWereScoped();

Btw, this is a very heavyweight way of testing, so I assume this is not something you need to do often.
good luck!

Dhanji.

naaka

unread,
Jun 13, 2009, 8:12:49 AM6/13/09
to google-guice
thanks dhanji.

but what other ways do i have for testing ?

i wanted to do some integration testing and i need to have everything
injected by the container.


nicola



On Jun 12, 6:32 pm, "Dhanji R. Prasanna" <dha...@gmail.com> wrote:
> You should bind @RequestScoped and @SessionScoped to your own mock Scope
> implementation. Then Guice will use this instead of its default HTTP scopes.
> In this case you want to omit the ServletModule in your test.
> MockRequestScope myMockScope = new MockRequestScope();
> bindScope(RequestScoped.class, myMockScope);
>
> //after tests
> myMockScope.assertThatThingsWereScoped();
>
> Btw, this is a very heavyweight way of testing, so I assume this is not
> something you need to do often.
> good luck!
>
> Dhanji.
>

Dhanji R. Prasanna

unread,
Jun 13, 2009, 12:51:10 PM6/13/09
to google...@googlegroups.com
You can simulate a request by instantiating GuiceFilter() and passing it a mock request and response. If you have previously installed a configured ServletModule, it will execute the servlets + filters registered in the GS2 pipeline.

Dhanji.
Reply all
Reply to author
Forward
0 new messages