多次尝试模拟或模拟 HttpResponse
我想对可能返回 HTTP 状态代码 (SERVICE_UNAVAILABLE) 的服务进行 JUnit 测试。对我来说,由于容器已扩大且尚未可达,因此这可能不可用。在这种情况下,我会让代码重试服务最多 3 次。
我可以模拟 HttpResponse 进行测试,但仅限于一种情况。 Mockito 是否可以创建一个模拟对象,在第一次尝试时返回一个内容,跟踪尝试次数并根据尝试次数返回不同的结果?我唯一能想到做的就是实现我自己的 HttpResponse。 Mockito 有更好的方法吗?
I'd like to do a JUnit test for a service that could return an HTTP status code (SERVICE_UNAVAILABLE). For me, this could be unavailable due to the container being scaled up and not yet reachable. In that case, I'm having the code retry the service up to 3 times.
I can mock an HttpResponse for testing, but only for one situation. Can Mockito create a mocked object that returns one thing on the first attempt, track the number of attempts and return a different result depending on the attempt number? The only thing I can think of doing is to implement my own HttpResponse. Is there a better approach with Mockito?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
存根连续调用(迭代器式存根)
https://www.javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#10
Stubbing consecutive calls (iterator-style stubbing)
https://www.javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#10