在我的http客户端上运行JUnit测试时不断出错 [英] Keep getting errors when running JUnit test on my http client

查看:105
本文介绍了在我的http客户端上运行JUnit测试时不断出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a client to send requests to an API, and I am trying to write a Junit test but i keep getting this error. Here is my client code which sends a request:

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());
            }
        });
    }
}

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

Here is the test class that i am using to test that the correct response is being sent back:

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();
        });
    } 
}

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

And this is the error that i keep getting when trying to run the test:

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

Any idea why these errors are popping up when i try to run the test?

推荐答案

Object answer(InvocationOnMock invocation)中而不是

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

您应该使用

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

这篇关于在我的http客户端上运行JUnit测试时不断出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆