【问题标题】:How to mock Spring's @Retryable attributes such as maxAttemps and delay in SpringBootTest如何在 SpringBootTest 中模拟 Spring @Retryable 属性,例如 maxAttempts 和 delay
【发布时间】:2020-11-25 14:29:27
【问题描述】:

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

@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 和延迟从10 秒到0.5 秒这个特定的测试用例。 我想这样做,以便在运行测试用例时它不应该等待很长时间,并且测试用例应该快速结束,同时还要测试重试功能。

【问题讨论】:

  • 你不能,除非你使它们可配置并使用特定的测试配置运行。
  • 使用带有属性占位符的...expression 变体。

标签: java spring spring-boot mockito spring-retry


【解决方案1】:

使用

@Retryable(maxAttemptsExpression = "${max.attempts:4}", 
        backoff = @Backoff(delayExpression = "${delay:10000}"))

并在您的测试用例中设置属性。

【讨论】:

  • 我差点忘了我能做到这一点。正在按照使用特定属性模拟 RetryProperties 的方式工作。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 2019-08-08
  • 2022-12-19
  • 1970-01-01
  • 2016-04-27
  • 2017-12-03
  • 1970-01-01
相关资源
最近更新 更多