繁体   English   中英

Mockito 模拟 retrytemplate.execute 并返回模拟响应

[英]Mockito mocking retrytemplate.execute and return mocked response

下面是我要测试的代码:

@Autowired
RetryTemplate retryTemplate;

MyResponse response;

response = retryTemplate.execute(new RetryCallback<Mono<MyResponse>, RuntimeException>() {
    public Mono<MyResponse> doRetry(RetryContext context) {
        return webclient.post.body(Mono.just(requestBodyObj), RequestBodyObj.class).retrieve().bodyToMono(MyResponse.class);
    }
});

下面是我正在尝试的测试用例,但得到了 Invalid use of matchers 异常。 另外,我不确定我下面的代码是否有效。 请指导:

MyResponse myResponseObj = new MyResponse();

when(retryTemplate.execute(any(RetryCallback.class))).thenAnswer(invocation -> {
        RetryCallback retry = invocation.getArgument(0);
        when(retry.doWithRetry(Mockito.any())).thenReturn(Mockito.eq(Mono.just(myResponseObj)))
        return retry.doWithRetry(null);
    });

PLS指导什么是这里的错误,我怎么能回到myResponseObj每当retry.doWithRetry被调用。

请查看下面的 retryTemplate 执行方法:

public final <T, E extends Throwable> T execute(RetryCallback<T, E> retryCallback) throws E {
        return this.doExecute(retryCallback, (RecoveryCallback)null, (RetryState)null);
}

当我们调用retryTemplate.execute(retryCallback)时,doExecute方法中的另外两个参数为null,所以基本上这就是你得到Invalid use of matchers异常的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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