在我的http客户端上运行JUnit测试时不断出错

时间:2019-01-02 07:35:29

标签: mockito client junit4 web-api-testing vertx-httpclient

我正在创建一个向API发送请求的客户端,并且我正在尝试编写Junit测试,但我一直收到此错误。 这是我发送请求的客户代码:

import io.netty.handler.codec.http.HttpResponse;
import io.vertx.core.AsyncResult;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;

public class LoginCoreClient {

    private WebClient webclient;
    private String requestURL;
    private static Logger logger = LoggerFactory.getLogger(LoginCoreClient.class);

    public LoginCoreClient(WebClient webclient, String requestURL) {
        this.webclient = webclient;
        this.requestURL = requestURL;
    }

    public void invokeCore(JsonObject request, java.util.function.Consumer<JsonObject> func){
        webclient.post(requestURL)
        .putHeader("content-type", "application/json")
        .sendJson(request, ar -> {
            if (ar.succeeded()) {
                logger.info("succeeded: " + ar.succeeded());
                logger.info("statusCode: " + ar.result().statusCode());
                logger.info("body: " + ar.result().body());
                logger.info("headers: " + ar.result().headers());
                JsonObject response = new JsonObject();
                // populate it
                func.accept(response);
             } else {
                logger.info("Executed: " + ar.cause());
            }
        });
    }
}

这是我用来测试是否已发送正确响应的测试类:

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*;
import java.awt.List;
import javax.xml.ws.Response;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import io.netty.util.concurrent.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;

@ExtendWith(VertxExtension.class)
public class LoginCoreTestTest {

    private LoginCoreTest client;

    //set up WebClient
    private WebClient createMockWebClient(JsonObject mockResponse) {
        WebClient mockWebClient = mock(WebClient.class);
        HttpRequest<Buffer> mockRequest = mock(HttpRequest.class);

        when(mockWebClient.post(any())).thenReturn(mockRequest);
        when(mockRequest.putHeader(any(), any())).thenReturn(mockRequest);
        doAnswer(new Answer() {

            @Override
            public Object answer(InvocationOnMock invocation) throws Throwable {
                // TODO Auto-generated method stub
                java.util.function.Consumer func = invocation.getArgument(1);
                func.accept(mockResponse);
                return null;
            }

        }).when(mockRequest).sendJson(any(), any());
        return mockWebClient;
    }

    @Test
    @DisplayName("Test response from client")
    public void test() { 
        //request being sent
        JsonObject request = new JsonObject().put("SSN", "123456789").put("Address", "123 main st").put("zip", "08888").put("dob", "012387");

        //expected response
        JsonObject response = new JsonObject().put("clientToken", "11oije311").put("clientID", "123ID");

        //test setup
        LoginCoreTest coreClient = new LoginCoreTest(createMockWebClient(response), "http://localhost:8080/core");

        //test steps
        coreClient.invokeCore(request, resp -> {
            assertEquals(resp.getString("clientToken"), response.getString("clientToken"));
            //end.finished();
        });
    } 
}

这是我尝试运行测试时不断遇到的错误: enter image description here

有人知道为什么当我尝试运行测试时会弹出这些错误吗?

1 个答案:

答案 0 :(得分:0)

Object answer(InvocationOnMock invocation)中代替

java.util.function.Consumer func = invocation.getArgument(1);

您应该使用

Handler<AsyncResult<HttpResponse<T>>> func = invocation.getArgument(1);