首页 > 解决方案 > 集成测试不适用于 @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```

标签: javaspring-bootjunitintegration-testingspring-test

解决方案


推荐阅读