3

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

@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可以将 a 序列化为 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);
    }
}
4

1 回答 1

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

由于某种原因,ratingDao 模拟没有设置。当我将该行移至 setUp 方法时,它起作用了。

于 2013-09-05T21:26:07.410 回答