gpt4 book ai didi

java - Dropwizard/Jersey 为资源返回 204

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:28:08 25 4
gpt4 key购买 nike

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

@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);
}
}

最佳答案

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

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

关于java - Dropwizard/Jersey 为资源返回 204,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18624855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com