默认情况下,Asyn重试Junit有3次未运行



我正在为可重试的失败场景进行junit测试。在我们升级到mockito和spring-boot的新版本之前,它一直工作得很好。我们已经更新到springboot 2.4.x,并开始看到这个问题。

Service.java

public class RetryTest 
{
@Autowired
private RetryTemplate retryTemplate;
@Async
private void retryTest(String in) {
retryTemplate.execute( invoke -> {
callMethod(in);
return null;
}, recoveryCallBack);
}
public void callMethod(String in) {
//some service call on failruew need to retry this.
someService.test(in);
}

}

单元测试:

@RunWith(SpringJUint4Runner.class)
Public class Test {
@InjectMocks
private RetryTesr retryTest;
@Mock
private RetryTemplate retryTemplate;

@Test(expected = ServiceException.class)
public void testFailure() {
when(someService.test(anyString())).ThenRetrun(new RuntimeException.class). ThenRetrun(new RuntimeException.class). ThenRetrun(new RuntimeException.class);

retryTest.retryTest(in);

}
}

当我在上面运行时,尽管retryTemplate默认为3次,但它只执行一次。应该是它应该执行3次,然后它应该像我们在recoveryCallBack中抛出的那样抛出一个服务异常。

有人能提出建议吗。

要测试任何spring功能,必须使用@SpringBootTest注释编写集成测试用例

@RunWith(SpringJUint4Runner.class)
@SpringBootTest
Public class Test {
@Autowired
private RetryTesr retryTest;
@MockBean
private RetryTemplate retryTemplate;
@Test(expected = ServiceException.class)
public void testFailure() {
when(someService.test(anyString())).ThenRetrun(new RuntimeException.class). ThenRetrun(new RuntimeException.class). ThenRetrun(new RuntimeException.class);
retryTest.retryTest(in);
}
}

它的wierd。在升级了spring-boot的版本之后。。any((不起作用,所以我必须使用精确的值来匹配mock。

最新更新