1

GET を期待する私の Web サービス リソース:

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

関連するクライアント:

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

私のテストでは、nullpointer を取得しましたがint responseCode = response.getStatus()webTarget. POST 応答では正しく実行できるように見えますが、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");
        }
    }

null 応答が返される理由はありますか?

4

1 に答える 1

0

どうやら私のクライアントのクローンは問題ないと思いますが、テストは間違っていました。もともと私はResponseクラスを嘲笑していましたが、代わりにそれをスパイし、モックにスパイを返させResponse.ok().build())、問題を解決しました。

    @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");
        }
    }
于 2020-04-01T16:41:06.477 に答える