简体   繁体   中英

Response is NULL when calling response.getStatus, is client not targeting what's in the webservice?

My webservice resource that expects a GET:

 @GET
 @Produces(MediaType.APPLICATION_JSON)
 @Path("/status")
 public Response checkNode() {
        boolean status = !NlpHandler.getHandlerQueue().isEmpty();
        status = status || !NlpFeeder.getInstance().getFiles().isEmpty();

        int statusCode = status ? 200 : 420;
        LOG.debug(
            "checking status - status: " + statusCode
            + ", node: " + this.context.getAbsolutePath()
        );

        return Response.status(statusCode).build();
}

The associated client:

public class NodeClient {
    private final Client client;
    private final WebTarget webTarget;

    public NodeClient(String uri) {
        this.uri = "some uri";
        client = ClientBuilder.newCLient();
        webTarget = client.target(uri);

    public synchronized boolean checkNode() throws IOException {
        String path = "status";
        Response response = webTarget
            .path(path)
            .request(MediaType.APPLICATION_JSON)
            .get(Response.class);

        int responseCode = response.getStatus();

        boolean success = responseCode == 200;
        if (!success && responseCode != 420) {
            checkResponse(response);
        }

        return success;
    }
}

In my test, I get a nullpointer at int responseCode = response.getStatus() and I'm fairly sure i'm not getting the response in the right way with webTarget . It looks like i'm able to do so correctly with POST responses but not when it's expecting a GET.

    @Test
    public void testCheckNode() throws Exception {
        Response response = Mockito.mock(Response.class);
        Mockito
            .doReturn(response)
            .when(builder)
            .get();

        NodeClient nodeClient;

        Mockito
            .doReturn(200)
            .when(response)
            .getStatus();

        try {
            boolean success = nodeClient.checkNode();

            Assert.assertTrue(success);
        } catch (IOException ex) {
            Assert.fail("No exception should have been thrown");
        }
    }

Any ideas why i'm getting a null response?

I think my client clode is fine apparently, the test was wrong. Originally I was mocking the Response class, now I spied it instead and made the mock return the spy of Response.ok().build()) and that fixed my problem.

    @Test
    public void testCheckNode() throws Exception {
        response = Mockito.spy(Response.class);
        Mockito
            .doReturn(Mockito.spy(Response.ok().build()))
            .when(builder)
            .get(Response.class);

        NodeClient nodeClient;
        PowerMockito
            .doNothing()
            .when(nodeClient, "handleResponse", Mockito.any());

        try {
            boolean success = nodeClient.checkNode();

            Assert.assertTrue(success);
        } catch (IOException ex) {
            Assert.fail("No exception should have been thrown");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM