如何从java sdk模拟aws sqs异步java函数

kq0g1dla  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(198)

我编写了一个sqs异步发送函数,将数据发送到sqs,如下所示:

CompletableFuture<SendMessageResult> future = new CompletableFuture<>();
        sqs.sendMessageAsync(request, new AsyncHandler<SendMessageRequest, SendMessageResult>() {
            @Override
            public void onError(Exception exception) {
                future.completeExceptionally(exception);
            }

            @Override
            public void onSuccess(SendMessageRequest request, SendMessageResult sendMessageResult) {
                future.complete(sendMessageResult);
            }
        });
        return future;

现在我尝试使用mockito模拟它,下面是模拟它的代码:

SendMessageResult sendMessageResult = new SendMessageResult();
        sendMessageResult.setMessageId("Dakota Johnson");
        sendMessageResult.setMD5OfMessageBody("It rocks");
        doReturn(CompletableFuture.completedFuture(sendMessageResult)).when(sqs).sendMessageAsync(any(SendMessageRequest.class), any());
        SendMessageRequest request = new SendMessageRequest();
        request.setMessageBody("Dakota Johsnon");
        request.setQueueUrl("None");
        SendMessageResult cf = sqsProducer.sendMessage(request).get();
        System.out.println(cf.getMessageId());

函数未完成,将进入无限循环。你能建议一下为什么它会进入无限循环,以及如何纠正这个问题吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题