Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法调用请求(Rest调用)_Java_Mockito_Resteasy - Fatal编程技术网

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

Java 无法调用请求(Rest调用),java,mockito,resteasy,Java,Mockito,Resteasy,我有rest服务(PUT),我打算对其进行单元测试。我正在使用RestEasy进行其余通话 这是我的资源: @Path("/my_person") public class MyResource { private MyDAO myDao; @Inject public MyResource(MyDao myDao) { this.myDao = myDao; } @PUT @Timed @Path("purchase_

我有rest服务(PUT),我打算对其进行单元测试。我正在使用RestEasy进行其余通话

这是我的资源:

@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();
    }
}
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()
处遇到以下错误,因为entity对象为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)

您是否尝试将
/
放在您的方法路径
@path(“
/
purchaseNumber/{purchaseNumber}/amount/{amount}/numberofu项目”)前面

我认为该框架误解了您的路径,并期望
mypersonpurchase\u number/

在响应中检查您的状态代码,查看是否实际返回200或404。

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

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

javax.ws.rs
jsr311 api
1.1.1
而不是

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

org.jboss.resteasy
jaxrs api
3.0.10.1决赛

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

我做了您建议的更改,但仍然得到了一个带有Response.getStatus()=200的NullPointerException。我认为它来自您调用rest api的方式
Response Response=resteasyClient.target(“http://localhost:9980/my_person/purchase_number/{purchaseNumber}/amount/{amount}/项目数“).request().buildPut(Entity.Entity(“{}”,MediaType.APPLICATION_JSON)).invoke()在模拟中,您希望看到'String purchaseNumber=“A57697DF”;字符串金额=“340”;当(myDao.getItems(purchaseNumber,amount))。然后返回(10);`但是,当您发出请求时,您意外地使用了
{string}
,请尝试将其替换为实际值。