使用 Mockito 模拟 post 请求引发 javax.ws.rs.ProcessingException: RESTEASY004655: 无法调用请求

Mocking a post request using Mockito raises javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request

我是 Mockito 的新手,我需要模拟一个 post 请求,以便我可以单独测试客户端。但是,无论我做什么,我都会得到 RESTEASY004655: Unable to invoke request 异常。这是我目前所拥有的简化版本。我有 class TestClassA,它向 api 发送 post 请求,如下所示:

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;

import com.fasterxml.jackson.databind.ObjectMapper;

public class TestClassA {

    public int moveActual(String path) throws IOException {

        Response resp = null;
        String newHome = "0" ;
        ResteasyClient client = new ResteasyClientBuilder().build();
        ResteasyWebTarget callTarget = client.target(path) ;

        Map<String, Object> request = new HashMap<>();
        request.put("operation", "dislocation");
        request.put("direction", "right");
        request.put("amount", "2");
        request.put("unit", "metric");
        String payload = new ObjectMapper().writeValueAsString(request);

        resp = callTarget.request().post(Entity.entity(payload, "application/json"));
        String outcome = resp.readEntity(String.class);
        ObjectMapper outcomeMapper = new ObjectMapper();
        @SuppressWarnings("unchecked")
        Map<String, Object> finalResponse = (Map<String, Object>) outcomeMapper.readValue(outcome, Map.class);

        if (finalResponse != null) {
            newHome = (String) finalResponse.get("coordinate");
        }
        return Integer.parseInt(newHome) ;
    }
}

我尝试使用下面的 mockito 测试模拟 post 请求:

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.BDDMockito.given;

import java.util.HashMap;
import java.util.Map;

import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import externalTests.TestClassA;

@RunWith(MockitoJUnitRunner.class)
public class DispositionMockTest {

    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
    ResteasyWebTarget targetMock;

    @InjectMocks
    TestClassA classA;


    @Test
    public void dispositionTest() throws Exception {
        // Given
        Map<String, Integer> outcomeMap = new HashMap<String, Integer>() ;
        outcomeMap.put("coordinate", 4) ;
        Response resp = Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity(outcomeMap).build() ;

        //  Builder req = DispositionMockTest.fakeRequest() ;
        //  given(req.post(ArgumentMatchers.any(Entity.class))).willReturn(fakeRequest()) ;

        given(targetMock.request().post(ArgumentMatchers.any(Entity.class))).willReturn(resp) ;

        // When
        int result = classA.moveActual("url to what has to run") ;

        // Then
        assertEquals(4, result);
    }
}

我还尝试在测试中使用两条注释掉的行来模拟请求方法,并从下面的方法中得到它 return 或多或少是伪造的 Builder。

public static Builder fakeRequest() {
    ResteasyClient httpClient = new ResteasyClientBuilder().build();
    ResteasyWebTarget target = httpClient.target("");
    Builder req = target.request() ;
    return req ;
}

但是我一直收到这个异常。

javax.ws.rs.ProcessingException: RESTEASY004655: Unable to invoke request: org.apache.http.client.ClientProtocolException
    at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:325)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:443)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:62)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.post(ClientInvocationBuilder.java:219)
    at externalTests.TestClassA.moveActual(TestClassA.java:33)
    at externalTests.DispositionMockTest.dispositionTest(DispositionMockTest.java:60)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.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.mockito.internal.runners.DefaultInternalRunner.evaluate(DefaultInternalRunner.java:46)
    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.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=15=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:77)
    at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:83)
    at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
    at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)
Caused by: org.apache.http.client.ClientProtocolException
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:187)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine.invoke(ApacheHttpClient4Engine.java:320)
    ... 33 more
Caused by: org.apache.http.ProtocolException: Target host is not specified
    at org.apache.http.impl.conn.DefaultRoutePlanner.determineRoute(DefaultRoutePlanner.java:71)
    at org.apache.http.impl.client.InternalHttpClient.determineRoute(InternalHttpClient.java:125)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
    ... 36 more

我尝试了很多我在网上找到的可能的解决方案,但我无法解决任何问题。如果有人知道我如何模拟 post 请求,我将非常感谢向他们学习。

因为你模拟了 ResteasyWebTarget 并且没有定义 request() 方法的 return 应该是什么,所以你得到了那个异常。

您应该首先模拟请求并将其 return 发送到 request() 方法,并将 post() 方法的行为定义为 mockedRequest

@Mock
private Invocation.Builder mockedRequest;

@Test
public void dispositionTest() throws Exception {
    when(targetMock.request()).thenReturn(mockedRequest);
    when(mockedRequest.post(any(Entity.class))).thenReturn(resp) ;
}

WebTarget request() doc

Invocation.Builder doc

对于可能需要它的人。我找不到使用 mockito 完成它的方法。我不得不转移到wiremock。在 wiremock 中,您只需记录一个成功的事务,创建一个 wiremock 服务器对象,然后让它模拟记录的行为。更容易和更快。