spring - How to mock multiple responses for same request using spring's MockRestServiceServer?
问题描述
Im using MockRestServiceServer for mocking http responses. In a specific scenario i call an endpoint two times and want a different response the second time.
But when i write a second expectation it's like it overwrites my first expectation.
How does one write multiple responses for the same request?
解决方案
经过一番研究,我发现了它:
当实例化一个 MockRestServiceServer 时,它默认获得一个 UnorderedRequestExpectationManager。通过 SimpleRequestExpectationManager 中的 Builder 进行更改,增加了对按定义顺序添加多个响应的支持。
private MockRestServiceServer createMockServerBy(Class<? extends
RestTemplate> requiredType) {
RestTemplate template = context.getBean(requiredType);
return MockRestServiceServer.bindTo(template).build(new
SimpleRequestExpectationManager());
}
推荐阅读
- python-3.x - 错误:IndexError:列表索引超出范围
- java - 如何根据 ListView 中的字符串值设置TextColor?
- c# - Xamarin Forms 项目中的错误 CS0433
- stackdriver - 有没有办法在 Stackdriver Logs Viewer 中查看周围的日志?
- matlab - 在 y 轴上旋转角度 theta 以欧拉角定向的关节
- javascript - 从目录路径获取对象数组中的目录路径作为字符串
- python - 测试 Google Street View Publish API 时出现“pkg_resources.DistributionNotFound: google-gax”
- c - 从函数返回的 char* 数组打印不正确的值(可能是内存管理)
- vba - 实现与二进制兼容性
- delphi - 带有MultiSelect的Firemonkey(Delphi)TListbox获取所选项目