我有一个我正在尝试测试的方法
@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/