Spring retry with retryTemplate - модульные тесты - PullRequest
0 голосов
/ 12 октября 2018

Я использую retryTemplate, и я не знаю, как сделать для этого модульные тесты.Может ли кто-нибудь помочь?

У меня есть этот фрагмент кода (например), и я хотел бы проверить, что первый раз, когда я вызываю Card.create, происходит сбой, но второй - это работает.

@Autowired
private RetryTemplate retryTemplate; 

public String registerCard(final String cardNumber) throws ApiException {  
    final Card response = retryTemplate.execute(new RetryCallback<Card, ApiException>() {
        @Override public Card doWithRetry(RetryContext retryContext) throws ApiException {
          return Card.create(map);
        }
     }) ;
     return response.get("number").toString();
}

RetryTemplate имеет maxAttemps = 2 и backOff = 1500.

С уважением.

1 Ответ

0 голосов
/ 12 октября 2018

В вашем тесте case добавьте реализацию RetryListener к RetryTemplate;используйте методы прослушивателя для отслеживания повторных действий.

public interface RetryListener {

    /**
     * Called before the first attempt in a retry. For instance, implementers
     * can set up state that is needed by the policies in the
     * {@link RetryOperations}. The whole retry can be vetoed by returning
     * false from this method, in which case a {@link TerminatedRetryException}
     * will be thrown.
     *
     * @param <T> the type of object returned by the callback
     * @param <E> the type of exception it declares may be thrown
     * @param context the current {@link RetryContext}.
     * @param callback the current {@link RetryCallback}.
     * @return true if the retry should proceed.
     */
    <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback);

    /**
     * Called after the final attempt (successful or not). Allow the interceptor
     * to clean up any resource it is holding before control returns to the
     * retry caller.
     * 
     * @param context the current {@link RetryContext}.
     * @param callback the current {@link RetryCallback}.
     * @param throwable the last exception that was thrown by the callback.
     * @param <E> the exception type
     * @param <T> the return value
     */
    <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable);

    /**
     * Called after every unsuccessful attempt at a retry.
     * 
     * @param context the current {@link RetryContext}.
     * @param callback the current {@link RetryCallback}.
     * @param throwable the last exception that was thrown by the callback.
     * @param <T> the return value
     * @param <E> the exception to throw
     */
    <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable);
}
...