无法调用请求(休息呼叫)

时间:2015-10-12 21:16:44

标签: java mockito resteasy

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

这是我的资源:

@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返回一个整数。

我的测试课程如下:

@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()收到以下错误,因为实体对象为null。那是否意味着它根本没有创建请求?

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)

2 个答案:

答案 0 :(得分:0)

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

我认为该框架误解了您的路径并期望my_personpurchase_number/。 检查您的状态代码是否响应,看看实际上是200还是404.

答案 1 :(得分:0)

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

        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
            <version>1.1.1</version>
        </dependency>

而不是

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>jaxrs-api</artifactId>
            <version>3.0.10.Final</version>
        </dependency>

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