简体   繁体   中英

Mockito mocking retrytemplate.execute and return mocked response

Below is the code I want to test:

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

Below is the test case I am trying, but getting Invalid use of matchers exception. Also, I am not sure if my below code would work. Pls guide:

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 guide what is the mistake here and how can I return myResponseObj whenever retry.doWithRetry is called.

Please have a view for below retryTemplate execute method:

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

When we call retryTemplate.execute(retryCallback), the other two arguments in doExecute method are null, so basically this is the reason why you get Invalid use of matchers exception.

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