java - 如何在 SpringBootTest 中模拟 Spring 的 @Retryable 属性,例如 maxAttemps 和延迟

标签 java spring spring-boot mockito spring-retry

我有一个我正在尝试测试的方法

@Retryable(value = {SocketTimeoutException.class},
             backoff = @Backoff(delay = 10000),
             maxAttempts = 4)
public String getNewString(String oldString) throws IOException{
   ...
}
我已经创建了它的测试用例,如下所示:
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestStrings {
  @Test(expected = SocketTimeoutException.class)
  public void testGetNewString() throws IOException {
     ...
  }
一切正常,测试用例运行 4 次,延迟 10 秒。但是我想更改@Retryable 的属性,即对于这个特定的测试用例,maxAttempts 从 4 到 2 和延迟从 10s 到 0.5s。
我想这样做是为了在运行测试用例时,它不应该等待很长时间,并且测试用例应该很快结束,同时还测试重试功能。

最佳答案

@Retryable(maxAttemptsExpression = "${max.attempts:4}", 
        backoff = @Backoff(delayExpression = "${delay:10000}"))
并在您的测试用例中设置属性。

关于java - 如何在 SpringBootTest 中模拟 Spring 的 @Retryable 属性,例如 maxAttemps 和延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63264737/

相关文章:

java - 为什么反序列化时不使用用户定义的serialVersionUID?

Java - 如何插入最终列表

jquery datepicker spring mvc 日期格式

spring-boot - SpringBoot JPA错误

spring-boot - Spring Boot异步请求处理任务执行器配置

java - 使用H2db实现springboot时出错

java - 冲突版本警告(JavaFX、JDK、JRE)

java - 如何在 Java 小程序中正确指定代码库和存档?

spring - 使用当前 httpclient (4.x) 的 RestTemplate 基本或摘要身份验证

spring - 错误未能将代码转换为 token