如何模拟 REST 服务?
我有一个 maven/mule/spring 开发环境,我在其中构建 REST 服务。我还有一系列 TestNG 测试来验证这些服务。我还希望能够更改服务的响应,返回特定信息或引发异常。这是我可以自动测试更广泛的服务行为。我认为模拟服务是最好的方法,但我找不到任何关于如何模拟 REST 服务的好信息。
我可以查看有关如何模拟 REST Web 服务的任何材料吗?
--更新---
我想我应该添加一个例子来使问题更加具体。如果我有以下设置:
testA calls serviceA, which then calls serviceB
如果 serviceA 应该返回一个 web 异常到 testA 如果 serviceB 响应错误,我想将一个 mockedServiceB 注入到系统中进行测试,其中 mockedServiceB 总是返回一个错误:
testA calls serviceA, which then calls mockedServiceB (which always returns an error to serviceA)
I have a maven/mule/spring development environment that I build REST services within. I also have a series of TestNG tests to validate these services. I also want the ability to alter the responses from the services, either returning specific information or throw an exception. This was I can automatically test broader behaviours of the services. I figured that mocking the services would be the best approach, but I cannot find any good information on how to mock a REST service.
Is there any material I can review on how to mock a REST web service?
--Update---
I thought I would add an example to make the problem more concrete. If I have the following setup:
testA calls serviceA, which then calls serviceB
If serviceA should return a web exception to testA if serviceB responds with an error, I would like to inject a mockedServiceB in to the system for the test where mockedServiceB always returns an error:
testA calls serviceA, which then calls mockedServiceB (which always returns an error to serviceA)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,并编写了一个用于模拟 REST 服务的小库: https://github.com/mkotsur/restito< /a>.
你可以尝试一下。
I had the same problem and wrote a small lib for mocking REST services: https://github.com/mkotsur/restito.
You can give it a try.
一般来说,我会将 Mule 配置分段为每个片段一个服务,然后在测试时加载真实服务 A 片段和测试服务 B 片段。测试服务 B 将使用 Mule test: 组件来模拟好的或坏的回报。
Generally speaking I would fragment my Mule configuration to have one service per fragment then load real service A fragment and a test service B fragment at test time. Test service B would use the Mule test:component to simulate good or bad returns.