简体   繁体   English

将REST呼叫模拟到另一个休息服务

[英]Mocking REST call to another rest-service

I've written a REST proxy that calls a REST service 我已经编写了一个REST代理来调用REST服务

@RequestMapping(value = { "session" }, method = {RequestMethod.GET)
    @ResponseBody
    public String sesionGet(HttpServletRequest request, HttpServletResponse httpServletResponse) {

            //call rest webservice    
            PostResponse postRequest = IncomeAccessUtils.postRequest(/*call web esrvice*/);

I need to do an integration test on that proxy but sometimes the server at which the external web service deployed on is down. 我需要对该代理进行集成测试,但有时部署外部Web服务的服务器已关闭。 Can I mock a response from webservice call ? 我可以模拟来自webservice调用的响应吗? I am using spring-test-mvc and/or rest-assure frameworks for testing. 我正在使用spring-test-mvc和/或rest-assure框架进行测试。

Yes there are several way of doing this in Java. 是的,在Java中有几种方法可以做到这一点。

Without much details I would recommend looking at betamax - it records any network integration and saves it for replay later. 如果没有太多细节,我建议您看一下betamax-它记录任何网络集成并保存以供以后重播。 You can pass in whethere you want to run live or use the recording. 您可以传递要直播或使用录音的信息。 (In one project we ran against real servers in the CI env but used version controlled recordings when running tests locally.) (在一个项目中,我们遇到了CI env中的真实服务器,但是在本地运行测试时使用了版本控制的记录。)

SoapUI is another tool capable of mocking a web server. SoapUI是另一个能够模拟Web服务器的工具。 This may be a good choice if you work with WSDL and/or SOAP. 如果您使用WSDL和/或SOAP,这可能是一个不错的选择。 (Poor you! :-) ) There are numerous such alternatives for rest out there but I don't have it fresh in my head. (可怜!:-))有很多这样的选择可以在那里休息,但是我没有新鲜的想法。

A simplistic alternativ is to run pythons simple HTTP server: python -m SimpleHTTPServer 8888 . 一个简单的替代方法是运行pythons简单的HTTP服务器: python -m SimpleHTTPServer 8888 It just serves whatever is in the directory where it was started. 它只提供启动目录中的内容。 Put your reponses there as files and you are good to go. 将您的回复作为文件放在那里,您一切都好。 (Work with simple stuff but if headers etc is important not so good. Also not good at simulating failed calls.) (使用简单的东西,但是如果标头等很重要,那就不好了。同样也不擅长模拟失败的调用。)

Finally if you are fine with not making the web call at all you can have mocks on the inside. 最后,如果您完全不进行网络通话,则可以在内部进行模拟。 If you use a dependency injection framework like Spring it is easy to run a version with mocked implementations of a backend call instead of the real one. 如果您使用像Spring这样的依赖注入框架,则很容易使用模拟的后端调用实现来运行一个版本,而不是真实的版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM