Keep getting errors when running JUnit test on my http client

孤街浪徒 提交于 2019-12-13 03:56:01

问题


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?


回答1:


In Object answer(InvocationOnMock invocation) instead of

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

you should use

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


来源:https://stackoverflow.com/questions/54002762/keep-getting-errors-when-running-junit-test-on-my-http-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!