[英]Retryable annotation - Junit5 - Mockito - is it possible
是否可以使用 Junit 5 mockito 为可重试注释编写单元测试?
我有一个只有一种方法的服务接口,它从远程 url 下载文件
@service
interface downloadpdf{
@Retryable(value = { FileNotFoundException.class, HttpClientErrorException.class }, maxAttempts = 5, backoff = @Backoff(delay = 1000))
public string downloadpdffile(string remoteurl, string pdfname);
}
我尝试过引用站点并发现使用 Spring4JunitRunner 实现来测试重试。 对实施感到困惑。 是否可以使用 Junit 5 mockito 为可重试注释编写单元测试? 您能否在这里详细说明解决方案?
您需要使用@SpringJUnitConfig
(相当于 JUnit4 运行程序)。 或@SpringBootTest
,因为您正在使用 Boot。
@Retryable
仅适用于由 Spring 管理的 bean - 它将 bean 包装在代理中。
@SpringBootApplication
@EnableRetry
public class So71849077Application {
public static void main(String[] args) {
SpringApplication.run(So71849077Application.class, args);
}
}
@Component
class RetryableClass {
private SomeService service;
void setService(SomeService service) {
this.service = service;
}
@Retryable
void retryableMethod(String in) {
service.callme();
throw new RuntimeException();
}
@Recover
void recover(Exception ex, String in) {
service.failed();
}
}
interface SomeService {
void callme();
void failed();
}
@SpringBootTest
class So71849077ApplicationTests {
@MockBean
SomeService service;
@Test
void testRetry(@Autowired RetryableClass retryable) {
SomeService service = mock(SomeService.class);
retryable.setService(service);
retryable.retryableMethod("foo");
verify(service, times(3)).callme();
verify(service).failed();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.