javaspringspring-retry

Unable to test Spring Retryable annotation


I am trying to test Spring's @Retryable annotation but so far I have not been able to do so. Elsewhere in the project I have seen that retries work, but I have been asked to review other methods and have seen that in some cases it is not clear if retrying is done.

I have a service which calls a method in an accessor.

  1. MyService.java:
String serviceMethod(String parameter);
  1. MyServiceImpl.java:
@Override
public String serviceMethod(final String parameter) {
   return this.myAccessor.accessorMethod(parameter);
}
  1. In the accessor, I have the @Retryable annotation set as follows:
@Retryable(include = {UncategorizedSQLException.class, SQLException.class}, maxAttemptsExpression = "${retry.maxAttempts}", listeners = {"customRetryListener"})
public String accessorMethod(final String parameter) {
   try {
       ...
   } catch (UncategorizedSQLException exception) {
       throw exception;
   } catch (final Exception exception) {
       return null;
   }
}

Therefore, trying to test if the method is reattempted I am doing a test, where at the top of the class I have set:

@EnableRetry
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-config.xml"})
public class myServiceTest {
   ...
}

And within that class, I created a test:

@Test
public void serviceMethodRetryTest() {
   try {
       this.myService.serviceMethod(this.parameter);
   } catch (UncategorizedSQLException exception) {
       log.error("Error while executing test: {}", exception.getMessage());
   }
}

But when launching the test the first exception occurs and it stops, I don't see it retrying.

I have tried to follow this and this but with no success, read the documentation, follow some tutorials on the internet but I do not know why in some scenarios the @Retryable works and in others it does not seem to work.

I have seen about the proxy in Spring with the Spring AOP approach, that it does not work if used in private method and that for the proxy to work you have to call it from another bean (which is already done).

What I am doing wrong?


Solution

  • After some more digging after Gary's answer and checking that the configuration was correct, in the end it was a problem related to the IDE and my way of testing the annotation, it had nothing to do with Spring.