Dropwizard / Jersey为资源返回204

时间:2013-09-04 22:28:23

标签: java jersey dropwizard

我正在尝试为资源设置测试:

@Override
protected void setUpResources() throws Exception {
    when(ratingDao.getRating("karan")).thenReturn(rating);
    addResource(new RatingResource(ratingDao));
}

@Test
public void testRatingsGetsTracks() throws Exception{
    assertThat(client().resource("/ratings").queryParam("username", "karan").get(Rating.class)).isEqualTo(rating);
    verify(ratingDao).getRating("karan");
}

但是,我得到了204:

1 > GET /ratings?username=karan
1 > 

23:18:24.705 [main] INFO  c.s.j.a.c.filter.LoggingFilter - 1 * Server out-bound response
1 < 204
1 < Content-Type: application/json
1 < 

23:18:24.708 [main] INFO  c.s.j.t.f.s.c.i.InMemoryTestContainerFactory$InMemoryTestContainer - Stopping low level InMemory test container

com.sun.jersey.api.client.UniformInterfaceException: Client response status: 204
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:540)
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:517)
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:684)
    at com.sun.jersey.api.client.WebResource.get(WebResource.java:191)
    at RatingResourceTest.testRatingsGetsTracks(RatingResourceTest.java:47)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我不知道为什么。我测试过Rating可以序列化为json。

以下是资源的实施:

@Path("/ratings")
@Produces(MediaType.APPLICATION_JSON)
public class RatingResource {
    private RatingDAO ratingDao;

    public RatingResource(RatingDAO ratingDao) {
        this.ratingDao = ratingDao;
    }

    @GET
    @Timed
    public Rating getRating(@QueryParam("username") String username)  {
        return ratingDao.getRating(username);
    }
}

1 个答案:

答案 0 :(得分:2)

@Override
protected void setUpResources() throws Exception {
    when(ratingDao.getRating("karan")).thenReturn(rating);
    addResource(new RatingResource(ratingDao));
}

由于某种原因,没有设置ratingDao mock。当我将该行移动到setUp方法时,这是有效的。