首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法调用请求(Rest调用)

无法调用请求(Rest调用)
EN

Stack Overflow用户
提问于 2015-10-13 05:16:44
回答 2查看 482关注 0票数 0

我有rest服务(PUT),我打算对它进行单元测试。我使用RestEasy进行Rest调用。

这是我的资源:

代码语言:javascript
运行
复制
@Path("/my_person")
public class MyResource {
    private MyDAO myDao;

    @Inject
    public MyResource(MyDao myDao) {
        this.myDao = myDao;
    }

    @PUT
    @Timed
    @Path("purchase_number/{purchaseNumber}/amount/{amount}/number_of_items")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getItems(@PathParam("purchaseNumber") String purchaseNumber, 
                             @PathParam("amount") String amount) {
       return Response.ok(String.format("{\"items\": %d}", myDao.getItems(purchaseNumber, amount))).build();
    }
}

myDao.getItems返回一个整数。

我的Test类如下:

代码语言:javascript
运行
复制
@RunWith(MockitoJUnitRunner.class)
public class MyResourceTest {
    @Mock
    private MyDao myDao;

    @InjectMock
    private MyResource myResource;

    private TJWSEmbeddedJaxrsServer server;
    private ResteasyClient resteasyClient;

    @Before
    public void startServer() throws IOException {

       resteasyClient = new ResteasyClientBuilder().build();
       server = new TJWSEmbeddedJaxrsServer();
       server.setPort(PORT);
       server.setBindAddress("localhost");
       server.getDeployment().getResources().add(myResource);
       server.start();

    }

    @After
    public void stopServer() {
       if (server != null) {
          server.stop();
          server = null;
       }
    }

    @Test
    public void testWhenValidInputGiven() {
       String purchaseNumber = "A57697DF";
       String amount = "340";
       when(myDao.getItems(purchaseNumber, amount)).thenReturn(10);

       Response response = resteasyClient.target("http://localhost:9980/my_person/purchase_number/{purchaseNumber}/amount/{amount}/number_of_items").request().buildPut(Entity.entity("{}", MediaType.APPLICATION_JSON))).invoke();

       String text = response.getEntity().toString();
       assertEquals(200, resp.getStatus());
       assertEquals("{\"items\": 10}", text);
    }

}

我在String text = response.getEntity().toString()得到以下错误,因为实体对象为空。那么这是否意味着它根本没有创建请求呢?

代码语言:javascript
运行
复制
java.lang.NullPointerException
    at resources.MyResourceTest.testWhenValidInputGiven(MyResourceTest.java:126)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

    at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:427)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:376)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
    at org.jboss.resteasy.plugins.server.tjws.TJWSServletDispatcher.service(TJWSServletDispatcher.java:40)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at Acme.Serve.Serve$ServeConnection.runServlet(Serve.java:2331)
    at Acme.Serve.Serve$ServeConnection.parseRequest(Serve.java:2285)
    at Acme.Serve.Serve$ServeConnection.run(Serve.java:2057)
    at Acme.Utils$ThreadPool$PooledThread.run(Utils.java:1402)
    at java.lang.Thread.run(Thread.java:745)
EN

回答 2

Stack Overflow用户

发布于 2015-10-13 05:26:16

您是否尝试过将/放在方法路径@Path("/purchase_number/{purchaseNumber}/amount/{amount}/number_of_items")的前面

我认为框架误解了你的路径和期望的my_personpurchase_number/。检查你的响应状态码,看看是否真的得到了200或404。

票数 0
EN

Stack Overflow用户

发布于 2015-10-14 09:52:31

这个问题的解决方案是我使用

代码语言:javascript
运行
复制
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
            <version>1.1.1</version>
        </dependency>

而不是

代码语言:javascript
运行
复制
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>jaxrs-api</artifactId>
            <version>3.0.10.Final</version>
        </dependency>

两者都有javax.ws.rs.core.Response类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33090351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档