无法获取要在Springboot单元测试中执行的reslient4j重试注释



我正试图编写一个单元测试,用@retry注释验证我的reslient4j应用程序,但它根本没有重试。当我运行它时,代码可以工作,只是无法对它进行单元测试。。有什么想法吗?

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

在单元测试中需要做的是自动连接RetryRegistry,然后使用它来获取为您的服务方法配置的Retry对象,然后使用Retry执行它。此外,请确保在测试类中包含Retry Auto配置。像这样:

@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

最新更新