简体   繁体   中英

Unable to get resilience4j retry annotation to execute in Springboot unit test

I'm trying to write a unit test to validate my resilience4j app with the @retry annotation, but it's not retrying at all. The code works when I run it, just can't get it unit tested.. Any ideas?

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = {ClassImTesting.class})
public class MyCoolTest {

    @Autowired
    private MyClass thing;


    @Test
    public void myTest() throws TransformationException {
        thing.transform(null);
    }

ClassImTesting

  @Override
  @Retry(name = "transformer", fallbackMethod = "handleFailure")
  public void transform(Thing record)
      throws TransformationException {
    return this.transform(record.value());
  }

public void handleFailure(Thing record, Throwable t) {
   // stuff
}

What needs to be done in the unit test is auto wire the RetryRegistry, then use it to grab the Retry object that is configured for your service method, then execute it using the retry. Also make sure to include the Retry Auto config on the test class. Like this:

@SpringBootTest(classes = {RetryAutoConfiguration.class, ClassImTesting.class})
public class MyCoolTest {

  @Autowired
  private MyClass thing;

  @Autowired
  private RetryRegistry registry;

 @Test
 public void myTest() throws TransformationException {
     final Retry transformerRetry = registry.retry("transformer");
     transformerRetry.executeSupplier(() -> thing.transform(null));
  }
}

https://github.com/resilience4j/resilience4j/blob/master/resilience4j-retry/src/test/java/io/github/resilience4j/retry/internal/SupplierRetryTest.java#L90

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