可重试注释-Junit5 -Mockito-是否可能
是否可以使用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编写单元测试进行重试注释?您能在这里详细说明解决方案吗?
Is it possible to write unit test using Junit 5 mockito for retryable annotations?
I am having a service interface which has only one method, which downloads the file from remote url
@service
interface downloadpdf{
@Retryable(value = { FileNotFoundException.class, HttpClientErrorException.class }, maxAttempts = 5, backoff = @Backoff(delay = 1000))
public string downloadpdffile(string remoteurl, string pdfname);
}
I have tried referring sites and found using Spring4JunitRunner implementation to test retry. Got confused with implementation. Is it possible to write unit test using Junit 5 mockito for retryable annotations?. Could you please elaborate on the solution here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用
@SpringJunitConfig
(这等同于Junit4 Runner)。或@springboottest
在使用启动时。@retryable
仅与Spring管理的Bean一起使用 - 它将Bean包裹在代理中。You need to use
@SpringJUnitConfig
(which is the equivalent of the JUnit4 runner). Or@SpringBootTest
as you are using Boot.@Retryable
only works with beans managed by Spring - it wraps the bean in a proxy.我也尝试使用 Junit5 来实现这一点。
尝试了各种选择,但这没有帮助。然后在谷歌搜索几个小时后,得到以下链接,这有助于成功。
https:// /doctorjw.wordpress.com/2022/04/29/spring-testing-a-single-bean-in-junit-5-springextension/
参考代码下面,详细解释请参考博客。
我发现,如果我这样声明我的测试类:
您会注意到包含另外 1 个类,TestingUtils.class。这个类看起来像:
所有学分都归博客作者所有。
I was also trying to implement this using Junit5.
Tried various options but that didn't help. Then after googling for few hours, got the following link and it helped to succeed.
https://doctorjw.wordpress.com/2022/04/29/spring-testing-a-single-bean-in-junit-5-springextension/
Reference code below, for detailed explanation, please refer the blog.
What I’ve discovered is, if I declare my test class this way:
You will notice the inclusion of 1 other class, TestingUtils.class. This class looks like:
All credits goes to the author of the blog.