模拟 Java Web 服务的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1042728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to mock java web service
提问by aaimnr
I have to mock quite complicated java web service and I'm searching for the right solution. One way to do it would be to use Soap UI, but I need something that would be able to modify server state ie. one request would affect future requests.
我必须模拟相当复杂的 java web 服务,我正在寻找正确的解决方案。一种方法是使用 Soap UI,但我需要一些能够修改服务器状态的东西,即。一项请求会影响未来的请求。
In this particular case it could be done quickly by saving serialized objects to disk and sometimes spawning asynchronous responses to the originating client webservice.
在这种特殊情况下,可以通过将序列化对象保存到磁盘并有时生成对原始客户端 Web 服务的异步响应来快速完成。
Those two requirements are preventing me from using SoapUI - the groovy logic would become quite complicated and probably hard to mantain.
这两个要求阻止了我使用 SoapUI - 常规逻辑会变得非常复杂并且可能难以维护。
My questions:
我的问题:
1) Are there any other SoapUI advantages in this context (eg. easy migration to new version of wsdl) over custom java mock implementation?
1)在这种情况下(例如,轻松迁移到新版本的 wsdl)与自定义 java 模拟实现相比,是否还有其他 SoapUI 优势?
2) What would be most appropriate way to generate the webservice from wsdl and still be able too hook up with some custom functionality, ie. by attaching some hooks that would be editable in seperate files (to facilitate further ws code regeneration from updated wsdl)?
2)从 wsdl 生成 webservice 的最合适方法是什么,并且仍然能够与一些自定义功能挂钩,即。通过附加一些可在单独文件中编辑的钩子(以促进从更新的 wsdl 进一步生成 ws 代码)?
采纳答案by Micha? Niklas
For simple mocks I use soapUI, while for more complicated when state must change between request I use simple web service emulator written in Python. Such emulator use reply templates created from real web service or responses I created in soapUI. This way I can control all logic.
对于简单的模拟,我使用soapUI,而对于更复杂的状态必须在请求之间更改时,我使用用Python 编写的简单Web 服务模拟器。此类模拟器使用从真实 Web 服务创建的回复模板或我在 soapUI 中创建的响应。这样我就可以控制所有的逻辑。
Emulator for my last project has 300+ lines of Python code, but for previous, much simplier, it was ~150 lines of Python code.
我上一个项目的模拟器有 300 多行 Python 代码,但对于以前的更简单的项目,它只有约 150 行 Python 代码。
回答by Romain
回答by Nick Holt
Presumably you're using some sort of generated stub in your client? You should mock stub with one of the mocking APIs (JMock or EasyMock) and inject the mock into the class-under-test.
大概您在客户端中使用了某种生成的存根?您应该使用其中一种模拟 API(JMock 或 EasyMock)模拟存根并将模拟注入到被测类中。
On the server-side test that class that handles the call, injecting mocks of any objects it might use to do its job.
在服务器端测试处理调用的类,注入它可能用来完成其工作的任何对象的模拟。
As an aside you should strive to keep all the calls in a unit test local (in-process). It makes it easy to control return values from any objects the class-under-test depends on and when the test suite grows will help prevent the unit tests becoming a bottle neck in your build process.
顺便说一句,您应该努力将所有调用保留在本地(进程中)单元测试中。它可以轻松控制来自被测类所依赖的任何对象的返回值,并且当测试套件增长时,将有助于防止单元测试成为构建过程中的瓶颈。
With regards to generating a Java class(es) from WSDL Apache Axis has something called WSDL2Java, which generates the client stubs I mentioned earlier. This sort of utility is common in the web service frameworks but may have been replaced now since EJB3 web services introduced javax.xml.rpc.ServiceFactory
exists.
关于从 WSDL 生成 Java 类,Apache Axis 有一个叫做 WSDL2Java 的东西,它生成我之前提到的客户端存根。这种实用程序在 Web 服务框架中很常见,但现在可能已被替换,因为引入了 EJB3 Web 服务javax.xml.rpc.ServiceFactory
。
There's a tutorial on EJB3 web services and clients here (http://www.theregister.co.uk/2007/01/23/ejb_web_services/).
这里有一个关于 EJB3 Web 服务和客户端的教程 ( http://www.theregister.co.uk/2007/01/23/ejb_web_services/)。