我试图让这个GrizzlyResourceTest.java与 dropwizard v0.6.1 一起使用。我想使用 grizzly 作为测试容器的原因是因为 InMemoryTestContainer 不支持 Resource with injectable constructor,请参阅这个问题的详细信息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 时,我从控制台得到的错误是:
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
有任何想法吗?