Gwt-dispatch unit testing

I am trying to write some unit tests for the gwt-dispatch service with JUnit. I get the following error while passing a test with my debugger:

Error in custom provider, com.google.inject.OutOfScopeException: Unable to access object with scope. Either we are not currently inside the HTTP servlet request, or you may have forgotten to use com.google.inject.servlet.GuiceFilter as a servlet filter for this request.

I am going to simplify the code a bit here, I hope I am not going to do anything.

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();
    }
}
}

It is assumed that the service request will go through GuiceFilter and it looks like this filter is not set.

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

+3
2

, . , . , RequestScoped , RequestScoped , , .

GuiceFilter , HttpServletRequest GuiceFilter .

unit test . , .

, - , :

  • , bindScope(RequestScoped.class, new FakeScope). FakeScope Scope . , "" , . . wiki CustomScopes. , IMHO
  • ServletScopes.scopeRequest (Javadoc), . , Callable.
  • . Selenium. , , .

, HttpServletRequest HttpServletResponse. . . , - , , , .

1, SimpleScope Guic CustomScopes wiki:

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)
    }
  }
}
+5

AppSession : HttpAppSession MockAppSession. AppSession, HttpSession .

  • Guice HttpSession HttpAppSession. , , . .

  • MockAppSession HttpSession, HttpServletRequest Guice Http. , .

Guice AppSession :

bind(AppSession.class).to(MockAppSession.class)
bind(MockAppSession.class).in(Singleton.class)

.

0

Source: https://habr.com/ru/post/1785207/


All Articles