在 Java 中将 WireMock 与 SOAP Web 服务一起使用

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

问题描述

我对 WireMock 完全陌生.

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

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

只需将 SOAP XML 请求发送到不同的端点 (http://localhost:9001/endpoint1) 并获得罐头返回 XML 响应.但是 MockWrire 必须作为独立服务部署到专用服务器上,该服务器将充当提供模拟响应的中心位置.

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.

只是想要一些开始的建议.正如我所见,WireMock 更适合 REST Web 服务.所以我的疑问是:

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

1) 我是否需要将其部署到 Java Web 服务器或容器以充当始终运行的独立服务.我读到你可以通过使用来分拆

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) 我需要使用 MockWire API 吗?我需要为我的用例制作课程吗?就我而言,请求将通过 JUnit 测试用例触发以进行模拟.

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) 如何实现简单的 URL 模式匹配?如上所述,我只需要简单的模拟,即在向 http://localhost:9001/endpoint1

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) 我的用例是否有更好/更简单的框架?我读过 Mockable,但它对 3 名团队成员和免费层的演示域有限制.

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.

推荐答案

我是 WireMock 的创造者.

I'm WireMock's creator.

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

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.

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

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.

依次解决您的观点:1)如果您希望您的模拟在某处的服务器上运行,最简单的方法是运行您所描述的独立 JAR.我建议不要尝试将其部署到容器中 - 此选项仅在别无选择时才存在.

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.

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

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) 您需要以 3 种方式之一对其进行配置:1) Java API,2) 基于 HTTP 的 JSON,或 3) JSON 文件.3) 可能最接近您使用 SOAP UI 的习惯.

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) 查看 http://wiremock.org/stubbing.html 了解大量使用JSON 和 Java.由于 SOAP 倾向于绑定到固定的端点 URL,您可能需要 urlEqualTo(...).当我过去使用 SOAP 存根时,我倾向于在整个请求正文上进行 XML 匹配(请参阅 http://wiremock.org/stubbing.html#xml-body-matching).我建议投资编写一些 Java 构建器来发出您需要的请求和响应正文 XML.

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) 模拟服务器 和 Betamax 都是 WireMock 的成熟替代品,但 AFAIK 它们不提供任何更明确的 SOAP 支持.

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 服务一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:测试 Java 套接字 下一篇:在 Java 中模拟 URL

相关文章

最新文章