我正在尝试测试,如果发生异常,我的方法调用是否被重试了几次。如果我直接调用方法,它就可以了。但是如果我在单独的线程中调用它,我的测试方法会失败,它不会看到任何重试尝试。这就是我的方法:
public class MyClass {
@Inject
private ServiceClass service;
@Retryable(value = {Exception.class}, maxAttempts = 5, backoff = @Backoff(delay = 30000))
public void retryMethod(Integer id, double dId)
{
Thread remoteDIUpdater = new Thread(() -> {
service.updateIndex(id, dId);
}
);
remoteDIUpdater.setDaemon(true);
remoteDIUpdater.setName("RemoteDIUpdaterThread");
remoteDIUpdater.start();
}
@Recover
public void recover(Exception ex, Integer id, double dId)
{
logger.error("Error", ex);
}
}
这是我的测试课:
public class TestClass
{
@Inject
@InjectMocks
private MyClass myClass;
@Mock
private private ServiceClass service;
@Test
public void testMethod()
{
Integer id = 1;
double dId = 2d;
doThrow(Exception.class).when(service).retryMethod(id, dId);
myClass.updateBetDangerIndex(betId, dangerIndex);
verify(service, Mockito.times(5)).retryMethod(null, id, iId);
}
}
也许我的测试方法是错误的。但是如果我像这样简单地调用我的方法(没有线程):
@Retryable(value = {Exception.class}, maxAttempts = 5, backoff = @Backoff(delay = 30000))
public void retryMethod(Integer id, double dId)
{
service.updateIndex(id, dId);
}
,我的测试方法运行正常。错误是什么?