Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3695050
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:34:14+00:00 2026-05-19T04:34:14+00:00

I’m trying to write some unit tests for a gwt-dispatch service with JUnit. I’m

  • 0

I’m trying to write some unit tests for a gwt-dispatch service with JUnit. I’m getting the following error when stepping through the test with my debugger:

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.

I’m going to simplify the code a bit here — hopefully I’m not stripping out anything necessary.

import junit.framework.TestCase;
import net.customware.gwt.dispatch.client.standard.StandardDispatchService;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.ServletModule;
...

public class LoggedInServiceTest extends TestCase {

Injector i;
StandardDispatchService service;

protected com.google.inject.Injector getInjector() {
    return Guice.createInjector(new ServletModule(),
            new TestServletModule(),
            new ActionsHandlerModule(),
            new TestDispatchModule(),
            new OpenIdGuiceModule());

}

public void setUp() throws Exception {
    i = getInjector();
    service = i.getInstance(StandardDispatchService.class);
}

public void testNotLoggedIn() {
    try {
        GetProjectsResult result = (GetProjectsResult) service.execute(new GetProjectsAction());
        result.getSizeOfResult();
    } catch (Exception e) {
        fail();
    }
}
}

The service request is indeed supposed to be going through a GuiceFilter, and it looks like that filter is not being set.

Any ideas on what other setup needs to be done to register the filter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:34:15+00:00Added an answer on May 19, 2026 at 4:34 am

    The problem is just what it states. You are trying to access a scoped object, but you are not currently in the scope. Most likely, your test is asking the injector for a RequestScoped object or an object that has a RequestScoped object in the injection dependency tree, but the test didn’t do anything to enter the scope.

    Binding the GuiceFilter in the test doesn’t help, because your test isn’t trying to send an HttpServletRequest through GuiceFilter to a servlet.

    The best option would be to unit test your code. Create your classes in isolation, injecting mocks.

    Assuming you want to do some kind of integration test, you have three options:

    1. Have your test install a test module that called bindScope(RequestScoped.class, new FakeScope). The FakeScope class would implement Scope and have methods to enter and exit the scope. You may have to “seed” the scope with fake implementations of objects you depend on. See the Guice CustomScopes wiki page. This is the best option for integration tests, IMHO
    2. Use ServletScopes.scopeRequest (Javadoc) to run part of the test code inside of a simulated request scope. This gets a bit ugly since you need to pass a Callable.
    3. Do a full end-to-end test. Start your server and send it requests using Selenium. It’s really hard to get good coverage this way, so I would leave this to things that you really need a browser to test.

    Things might get a bit messy if the class you are testing depends indirectly on HttpServletRequest or HttpServletResponse. These classes can be challenging to setup correctly. Most of your classes should not depend on the servlet classes directly or indirectly. If that is not the case, you are either doing something wrong or you need to find a good action framework that allows you have most of your code not depend on these classes.

    Here’s an example of approach 1, using SimpleScope from the Guice CustomScopes wiki page:

    public class LoggedInServiceTest extends TestCase {
      private final Provider<StandardDispatchService> serviceProvider;
      private final SimpleScope fakeRequestScope = new SimpleScope();
      private final HttpServletRequest request = new FakeHttpServletRequest();
    
      protected Injector createInjector() {
        return Guice.createInjector(new FakeRequestScopeModule(),
                new LoggedInServiceModule();
      }
    
      @Override
      protected void setUp() throws Exception {
        super.setUp();
        Injector injector = createInjector();
        scope.enter();
        serviceProvider = injector.getProvider(StandardDispatchService.class);
      }
    
      @Override
      protected void tearDown() throws Exception {
        fakeRequestScope.exit()
        super.tearDown();
      }
    
      public void testNotLoggedIn() {
        fakeRequestScope.enter();
        // fill in values of request
        fakeRequestScope.seed(FakeHttpServletRequest.class, request);
    
        StandardDispatchService service = serviceProvider.get();
        GetProjectsAction action = new GetProjectsAction();
        try {
            service.execute(action);
            fail();
        } catch (NotLoggedInException expected) {
        }
      }
    
      private class FakeRequestScopeModule extends AbstractModule() {
        @Override
        protected void configure() {
          bind(RequestScoped.class, fakeRequestScope);
          bind(HttpServletRequest.class)
              .to(FakeHttpServletRequest.class)
              .in(RequestScoped.class)
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.