簡體   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