在Java中使用WireMock和SOAP Web服务 [英] Using WireMock with SOAP Web Services in Java

查看:264
本文介绍了在Java中使用WireMock和SOAP Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 WireMock 的新手。



<到目前为止,我一直在使用SOAPUI进行模拟响应。我的用例很简单:



只是向不同的端点发送SOAP XML请求( http:// localhost:9001 / endpoint1 )并获取预设的XML响应。但是MockWrire必须作为独立服务部署到专用服务器上,该服务器将作为模拟响应的中心位置。



只是想要一些启动建议。我可以看到WireMock更适合REST Web服务。所以我的怀疑是:



1)我是否需要将其部署到java Web服务器或容器中,以便始终运行独立服务。我读到你可以使用

  java -jar mockwire.jar --port [port_number] 

2)我是否需要使用MockWire API?我需要为我的用例制作课程吗?在我的例子中,请求将通过JUnit测试用例触发进行模拟。



3)如何实现简单的URL模式匹配?如上所述,我只需要简单的模拟,即在请求 http:// localhost:9001 / endpoint1



4)我的用例是否有更好/更容易的框架?我读到了关于Mockable的内容,但它对3个团队成员和免费套餐中的演示域有限制。

解决方案

我是WireMock的创建者。



我最近使用WireMock模拟客户端项目上的SOAP接口集合,所以我可以证明它是可能的。至于它是否比SOAP UI更好或更差,我会说有一些明确的优势,但有一些权衡。一个主要的好处是相对容易部署和编程访问/配置,以及对HTTPS和低级故障注入等方面的支持。但是,您需要做更多的工作来解析和生成SOAP有效负载 - 它不会像SOAP UI那样从WSDL生成代码/存根。



我的经验像SOAP UI这样的工具可以让你更快地开始,但是从长远来看,当你的测试套件变得越来越琐碎时,往往会导致更高的维护成本。



要解决你的问题反过来说:
1)如果你想让你的模拟器在某个服务器上运行,最简单的方法就是按照你的描述运行独立的JAR。我建议不要尝试将它部署到容器中 - 这个选项实际上只存在于没有其他选择的时候。



但是,如果你只是想运行集成测试或者完全自包含的功能测试,我建议使用JUnit规则。我会说,如果a)你将其他已部署的系统插入其中,或者b)你是从非JVM语言中使用它,那么在一个专用的进程中运行它是个好主意。



2)您需要以3种方式之一配置它:1)Java API,2)JSON over HTTP或3)JSON文件。 3)可能与您习惯使用SOAP UI的方式最接近。



3)参见 http://wiremock.org/stubbing.html 了解了许多使用JSON和Java的存根示例。由于SOAP倾向于绑定到固定端点URL,因此您可能需要 urlEqualTo(...)。当我过去使用SOAP时,我倾向于在整个请求体上进行XML匹配(参见 http://wiremock.org/stubbing.html#xml-body-matching )。我建议投资编写一些Java构建器来发出你需要的请求和响应体XML。



4)模拟服务器 Betamax 都是WireMock的成熟替代品,但是AFAIK他们不提供任何更明确的SOAP支持。


I am totally new to WireMock.

Until now, I have been using mock responses using SOAPUI. My use case is simple:

Just firing SOAP XML requests to different endpoints (http://localhost:9001/endpoint1) and getting canned XML response back. But MockWrire has to be deployed as a standalone service onto a dedicated server which will act a central location from where mock responses will be served.

Just wanted some starting suggestions. As I can see WireMock is more suitable towards REST web services. So my doubts are:

1) Do I need to deploy it to a java web server or container to act as always running standalone service. I read that you can just spin off by using

java -jar mockwire.jar --port [port_number]

2) Do I need to use MockWire APIs? Do I need to make classes for my use case? In my case, requests will be triggered via JUnit test cases for mocking.

3) How do I achieve simple URL pattern matching? As stated above, I just need simple mocking i.e get response when request is made to http://localhost:9001/endpoint1

4) Is there a better/easier framework for my usecase? I read about Mockable but it has restrictions for 3 team members and demo domain in free tier.

解决方案

I'm WireMock's creator.

I've used WireMock to mock a collection of SOAP interfaces on a client project quite recently, so I can attest that it's possible. As for whether it's better or worse than SOAP UI, I'd say there are some definite upsides, but with some tradeoffs. A major benefit is the relative ease of deployment and programmatic access/configuration, and support for things like HTTPS and low-level fault injection. However, you need to do a bit more work to parse and generate SOAP payloads - it won't do code/stub generation from WSDL like SOAP UI will.

My experience is that tools like SOAP UI will get you started faster, but tend to result in higher maintenance costs in the long run when your test suite grows beyond trivial.

To address your points in turn: 1) If you want your mocks to run on a server somewhere, the easiest way to do this is to run the standalone JAR as you've described. I'd advise against trying to deploy it to a container - this option really only exists for when there's no alternative.

However, if you just want to run integration tests or totally self-contained functional tests, I'd suggest using the JUnit rule. I'd say it's only a good idea to run it in a dedicated process if either a) you're plugging other deployed systems into it, or b) you're using it from a non-JVM language.

2) You'd need to configure it in one of 3 ways 1) the Java API, 2) JSON over HTTP, or 3) JSON files. 3) is probably closest to what you're used to with SOAP UI.

3) See http://wiremock.org/stubbing.html for lots of stubbing examples using both JSON and Java. Since SOAP tends to bind to fixed endpoint URLs, you probably want urlEqualTo(...). When I've stubbed SOAP in the past I've tended to XML match on the entire request body (see http://wiremock.org/stubbing.html#xml-body-matching). I'd suggest investing in writing a few Java builders to emit the request and response body XML you need.

4) Mock Server and Betamax are both mature alternatives to WireMock, but AFAIK they don't offer any more explicit SOAP support.

这篇关于在Java中使用WireMock和SOAP Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆