如何在SpringBootTest中模拟Spring的@Retryable属性,例如maxAttemps和delay

Par*_*kur 5 java spring mockito spring-retry spring-boot

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

@Retryable(value = {SocketTimeoutException.class},
             backoff = @Backoff(delay = 10000),
             maxAttempts = 4)
public String getNewString(String oldString) throws IOException{
   ...
}
Run Code Online (Sandbox Code Playgroud)

我已经创建了它的测试用例,如下所示:

@SpringBootTest
@RunWith(SpringRunner.class)
public class TestStrings {
  @Test(expected = SocketTimeoutException.class)
  public void testGetNewString() throws IOException {
     ...
  }
Run Code Online (Sandbox Code Playgroud)

一切运行良好,测试用例运行 4 次,延迟 10 秒。但我想更改@Retryable的属性,即对于这个特定的测试用例,maxAttempts从4到2,延迟从10s到0.5s。我想这样做,以便在运行测试用例时不应该等待很长时间,并且测试用例应该快速结束,同时还测试重试功能。

Gar*_*ell 6

使用

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

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