使用Grizzly作为dropwizard资源的测试容器

时间:2013-03-14 19:26:06

标签: jersey dropwizard

我试图让这个GrizzlyResourceTest.java使用dropwizard v0.6.1。我想使用grizzly作为测试容器的原因是因为InMemoryTestContainer不支持带有可注入构造函数的Resource,请参阅详细信息InMemoryTestContainer does not support Resource with injectable constructor

由于com.yammer.dropwizard.json.Json和com.yammer.dropwizard.bundles.JavaBundle不再在v0.6.1中,我只是注释掉与这些类相关的行。

  @Before
  public void setUpJersey() throws Exception {
    setUpResources();
    this.test = new JerseyTest(new GrizzlyWebTestContainerFactory()) {
      @Override
      protected AppDescriptor configure() {
        ClientConfig config = new DefaultClientConfig();

        // taken from DropwizardResourceConfig
        config.getFeatures().put(ResourceConfig.FEATURE_DISABLE_WADL, Boolean.TRUE);
        config.getSingletons().add(new LoggingExceptionMapper<Throwable>() { }); // create a subclass to pin it to Throwable
        config.getClasses().add(InstrumentedResourceMethodDispatchAdapter.class);
        config.getClasses().add(CacheControlledResourceMethodDispatchAdapter.class);

        for (Class<?> provider : providers) {
          config.getClasses().add(provider);
        }

        config.getSingletons().addAll(singletons);

        return new WebAppDescriptor.Builder("com.example.helloworld.resources").clientConfig(config).build();
      }
    };
    test.setUp();
  }

我的情况更复杂,因为我将HttpServletRequest注入我的资源类,如myMethod(@Context HttpServletRequest request)。所以这里我只使用dropwizard-example下的PersonResource。

QuickTest.java看起来像:

public class QuickTest extends GrizzlyResourceTest{
    @Test
    public void testGrizzly() throws Exception {
        ClientResponse rsp = client()
            .resource("http://localhost:9998/people").get(ClientResponse.class);
        Assert.assertEquals(200, rsp.getStatus());
    }
    @Override
    protected void setUpResources() throws Exception {
    }
}

当我运行QuickTest时,我从Console获得的错误是:

SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
Mar 14, 2013 7:14:11 PM com.sun.jersey.test.framework.spi.container.grizzly2.web.GrizzlyWebTestContainerFactory$GrizzlyWebTestContainer <init>
INFO: Creating Grizzly2 Web Container configured at the base URI http://localhost:9998/
Mar 14, 2013 7:14:11 PM org.glassfish.grizzly.http.server.NetworkListener start
INFO: Started listener bound to [localhost:9998]
Mar 14, 2013 7:14:11 PM org.glassfish.grizzly.http.server.HttpServer start
INFO: [HttpServer] Started.
Mar 14, 2013 7:14:11 PM org.glassfish.grizzly.servlet.WebappContext deploy
INFO: Starting application [TestContext] ...
Mar 14, 2013 7:14:11 PM org.glassfish.grizzly.servlet.WebappContext initServlets
INFO: [TestContext] Servlet [com.sun.jersey.spi.container.servlet.ServletContainer] registered for url pattern(s) [[/*]].
Mar 14, 2013 7:14:11 PM org.glassfish.grizzly.servlet.WebappContext deploy
INFO: Application [TestContext] is ready to service requests.  Root: [].
Mar 14, 2013 7:14:11 PM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for field: private javax.servlet.http.HttpServletRequest com.yammer.dropwizard.jersey.LoggingExceptionMapper.request

有什么想法吗?

0 个答案:

没有答案