Spring Retry Junit:使用自定义重试策略测试重试模板

我正在尝试测试使用自定义重试策略的重试模板。 为了做到这一点,我使用这个例子:

https://github.com/spring-projects/spring-retry/blob/master/src/test/java/org/springframework/retry/support/RetryTemplateTests.java#L57

基本上,我的目标是在我获得某些特定的http错误状态(例如http 500错误状态)时测试我的重试逻辑。

这是我的junit的xml上下文:

                

CustomRetryPolicy就像:

 public class CustomRetryPolicy extends ExceptionClassifierRetryPolicy { private String maxAttempts; @PostConstruct public void init() { this.setExceptionClassifier(new Classifier() { @Override public RetryPolicy classify(Throwable classifiable) { Throwable exceptionCause = classifiable.getCause(); if (exceptionCause instanceof HttpStatusCodeException) { int statusCode = ((HttpStatusCodeException) classifiable.getCause()).getStatusCode().value(); return handleHttpErrorCode(statusCode); } return neverRetry(); } }); } public void setMaxAttempts(String maxAttempts) { this.maxAttempts = maxAttempts; } private RetryPolicy handleHttpErrorCode(int statusCode) { RetryPolicy retryPolicy = null; switch(statusCode) { case 404 : case 500 : case 503 : case 504 : retryPolicy = defaultRetryPolicy(); break; default : retryPolicy = neverRetry(); break; } return retryPolicy; } private RetryPolicy neverRetry() { return new NeverRetryPolicy(); } private RetryPolicy defaultRetryPolicy() { final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy(); simpleRetryPolicy.setMaxAttempts(Integer.valueOf(maxAttempts)); return simpleRetryPolicy; } } 

我正在测试的Java类是:

 @RunWith( SpringJUnit4ClassRunner.class ) @ContextConfiguration(locations = {"classpath:my_context_for_test.xml"}) public class RetryTemplateTest{ @Autowired @Qualifier("retryTemplate_test") RetryTemplate retryTemplate_test; @Test public void testRetry() throws Throwable { Map<Class, Boolean> r = new HashMap(); r.put(HttpStatusCodeException.class, true); MockRetryCallback callback = new MockRetryCallback(); callback.setAttemptsBeforeSuccess(5); retryTemplate_test.execute(callback); assertEquals(5, callback.attempts); } private static class MockRetryCallback implements RetryCallback { private int attempts; private int attemptsBeforeSuccess; @SuppressWarnings("serial") @Override public Object doWithRetry(RetryContext status) throws HttpStatusCodeException { this.attempts++; if (this.attempts < this.attemptsBeforeSuccess) { System.out.println("I'm here: "+ this.attempts); throw new HttpStatusCodeException(HttpStatus.INTERNAL_SERVER_ERROR) { }; } return null; } public void setAttemptsBeforeSuccess(int attemptsBeforeSuccess) { this.attemptsBeforeSuccess = attemptsBeforeSuccess; } } } 

我究竟做错了什么? 我的理解是,使用回调,我正在嘲笑一个响应,并且我可以处理(使用我的自定义重试策略)该响应。 也

[UPDATE]

如果我尝试复制这个junit ,那么我得到了同样的例外。 最具体地说,当尝试在MockRetryCallback类中实例化exception时,它会失败:

  private Exception exceptionToThrow = new Exception(); 

我能够使用它:

 @RunWith( SpringJUnit4ClassRunner.class ) @ContextConfiguration(locations = {"classpath:test-context.xml"}) public class HttpRetryTest{ @Autowired @Qualifier("retryTemplate_test") RetryOperations retryTemplate_test; @Test public void testRetry() throws Throwable { Map, Boolean> r = new HashMap<>(); r.put(HttpStatusCodeException.class, true); MockRetryCallback callback = new MockRetryCallback(); MockRetryCallback.attemptsBeforeSuccess =5; retryTemplate_test.execute(callback); assertEquals(5, MockRetryCallback.attempts); } private static class MockRetryCallback implements RetryCallback { private static int attempts; private static int attemptsBeforeSuccess; @SuppressWarnings("serial") @Override public Object doWithRetry(RetryContext status) throws HttpStatusCodeException { MockRetryCallback.attempts++; if (MockRetryCallback.attempts <= MockRetryCallback.attemptsBeforeSuccess) { System.out.println("I'm here: "+ MockRetryCallback.attempts); throw new HttpStatusCodeException(HttpStatus.INTERNAL_SERVER_ERROR) { }; } return null; } } }