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)

理由がわかりません。Ratinga を 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 に答える