java - 集成测试不适用于 @Async 和 @Retryable
问题描述
我正在使用 JUNIT 编写集成测试。由于在测试中调用的方法之一中使用了@Retryable,我
org.springframework.retry.backoff.BackOffInterruptedException
在运行测试时得到了。当涉及的方法是 @Async 或 @Retryable 时,如何编写集成测试?
这里分别是测试类和服务类的代码——
public class TestEndpointIT {
@Autowired
EndpointService endpoint
@Test
void testMethod() throws DatatypeConfigurationException, ParseException {
ResponseMessage response = endpoint.testService(sm);
assertTrue(response.getResponse().getAckNakResponse() == ResponseType.ACK);
};
class EndpointService {
@Async
@Retryable(value = RuntimeException.class, maxAttempts = 3, backoff = @Backoff(delay = 30000))
@Override
public void testMethod() {
// Code
}
}...
Following is the stack trace for the same-
``` 2021-08-02 14:49:22.621 ERROR 121348 --- [ task-1] .a.i.SimpleAsyncUncaughtExceptionHandler : Unexpected exception occurred invoking async method: public void com.att.dtss.playlistadapter.service.ScheduleServiceImpl.processSchedule(com.att.dtss.playlistadapter.dto.ScheduleDto) throws javax.xml.datatype.DatatypeConfigurationException,java.text.ParseException
org.springframework.retry.backoff.BackOffInterruptedException: Thread interrupted while sleeping; nested exception is java.lang.InterruptedException: sleep interrupted
at org.springframework.retry.backoff.FixedBackOffPolicy.doBackOff(FixedBackOffPolicy.java:85) ~[spring-retry-1.3.1.jar:?]
at org.springframework.retry.backoff.StatelessBackOffPolicy.backOff(StatelessBackOffPolicy.java:36) ~[spring-retry-1.3.1.jar:?]
at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:347) ~[spring-retry-1.3.1.jar:?]
at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:209) ~[spring-retry-1.3.1.jar:?]
at org.springframework.retry.interceptor.RetryOperationsInterceptor.invoke(RetryOperationsInterceptor.java:119) ~[spring-retry-1.3.1.jar:?]
at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:163) ~[spring-retry-1.3.1.jar:?]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.8.jar:5.3.8]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:750) ~[spring-aop-5.3.8.jar:5.3.8]
at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115) ~[spring-aop-5.3.8.jar:5.3.8]
at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_291]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_291]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_291]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_291]
Caused by: java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method) ~[?:1.8.0_291]
at org.springframework.retry.backoff.ThreadWaitSleeper.sleep(ThreadWaitSleeper.java:31) ~[spring-retry-1.3.1.jar:?]
at org.springframework.retry.backoff.FixedBackOffPolicy.doBackOff(FixedBackOffPolicy.java:82) ~[spring-retry-1.3.1.jar:?]
... 12 more```
解决方案
推荐阅读
- code-organization - 合并拉取请求中的冲突
- java - 更新 MoneyTextView Android Studio
- objective-c - SKVideoNode 不考虑纵横比
- node.js - 如何使用 NodeJS 构建可执行文件
- javascript - 连接元素以形成 jQuery 类选择器时的语法错误
- javascript - 在另一个函数中使用函数回调
- c++ - 不控制派生类的线程安全工厂模式
- android - 如何在flutter中将第三方android库导入java文件
- c# - 将边框添加到仅在聚焦时可见的选择器?
- c# - 创建数据库表不起作用 -> 存在,无法打开