【发布时间】:2016-04-01 05:24:08
【问题描述】:
我正在尝试通过关注 https://jersey.java.net/documentation/latest/test-framework.html 来测试 Jax-rs 资源,
我正在使用容器jersey-test-framework-provider-jdk-http
我可以断言状态码。但是,当我尝试读取实体时,出现异常:
javax.ws.rs.ProcessingException: Unable to find a MessageBodyReader of content-type application/json and type class java.lang.String
at org.jboss.resteasy.core.interception.ClientReaderInterceptorContext.throwReaderNotFound(ClientReaderInterceptorContext.java:39)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:73)
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:50)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readFrom(ClientResponse.java:248)
at org.jboss.resteasy.client.jaxrs.internal.ClientResponse.readEntity(ClientResponse.java:181)
at org.jboss.resteasy.specimpl.BuiltResponse.readEntity(BuiltResponse.java:217)
我的资源类:
@Path("/")
public class SampleResource {
@GET
@Path("/health")
@Produces(MediaType.APPLICATION_JSON)
public String getServiceStatus() {
return "{\"Status\": \"OK\"}";
}
}
我的测试班:
public class TestSampleResource extends JerseyTest {
@Override
protected Application configure() {
return new ResourceConfig(SampleResource.class);
}
@Test
public void testHealthEndpoint() {
Response healthResponse = target("health").request(MediaType.APPLICATION_JSON).get();
Assert.assertEquals(200, healthResponse.getstatus()); // works
String body = healthResponse.readEntity(String.class);
Assert.assertEquals("{\"Status\": \"OK\"}", body);
}
}
有人可以帮忙吗?
【问题讨论】:
标签: java junit jersey jax-rs resteasy