在dropwizard 0.7资源测试中缺少消息体阅读器

时间:2014-05-02 07:09:52

标签: java unit-testing dropwizard

我正在尝试将旧的dropwizard-0.6项目迁移到dropwizard-0.7,我在资源单元测试中得到了着名的消息体阅读器错误。

ERROR [2014-05-02 09:52:59,873] com.sun.jersey.api.client.ClientResponse: A message body reader for Java class foo.Bar, and Java type class foo.Bar, and MIME media type application/octet-stream was not found
ERROR [2014-05-02 09:52:59,876] com.sun.jersey.api.client.ClientResponse: The registered message body readers compatible with the MIME media type are:
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
  com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
  com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
  com.sun.jersey.core.impl.provider.entity.EntityHolderReader
application/octet-stream ->
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.RenderedImageProvider

我以前用这样的旧dropwizard-0.6 ViewMessageBodyWriter添加ResourceTest

@Override
protected void setUpResources() throws Exception {
    ...
    addProvider(ViewMessageBodyWriter.class);
    ...
}

我无法引入旧的ViewMessageBodyWriter,因为它不再具有零参数构造函数:

public class BarResourceTest extends AuthenticatedResourceTest {
    static BarController barController = mock(BarController.class);

    @Mock
    Page<Bar> page;

    @ClassRule
    public static ResourceTestRule resource = ResourceTestRule.builder().addProvider(ViewMessageBodyWriter.class)
        .addResource(new BarResource(barController)).build();

    private Bar bar;

}

有人可以帮助dropwizard-0.7中的资源测试吗?

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

@Provider
@Produces(application/octet-stream)
public class YourTestBodyWriter implements MessageBodyWriter<foo.Bar> {

    private static final Logger LOG = LoggerFactory.getLogger(foo.BarBodyWriter.class);

    @Override
    public boolean isWriteable(
        Class<?> type,
        Type genericType,
        Annotation[] annotations,
        MediaType mediaType)
    {
        return foo.Bar.class.isAssignableFrom(type);
    }

    @Override
    public long getSize(
        Bar t,
        Class<?> type,
        Type genericType,
        Annotation[] annotations,
        MediaType mediaType)
    {
        return -1;
    }

    @Override
    public void writeTo(
        Bar t,
        Class<?> type,
        Type genericType,
        Annotation[] annotations,
        MediaType mediaType,
        MultivaluedMap<String, Object> httpHeaders,
        OutputStream entityStream) throws IOException, WebApplicationException
    {
        String message = t.someMethod()
        entityStream.write(message.getBytes(Charsets.UTF_8));
        LOG.info(message);
    }

}

在您的应用中添加run():

// Serializer
environment.jersey().register(new YourTestBodyWriter ());   

这是您申请中的正常方式。对于junit测试,这可能有效(未经我测试!):

   @ClassRule
    public static ResourceTestRule resource = ResourceTestRule.builder().addProvider(ViewMessageBodyWriter.class)
        .addResource(new BarResource(barController)).addProvider(new YourTestBodyWriter()).build();