在 Java 中将 WireMock 与 SOAP Web 服务结合使用 [英] Using WireMock with SOAP Web Services in Java

查看:32
本文介绍了在 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 APIs 吗?我需要为我的用例创建类吗?就我而言,请求将通过 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) JSON over HTTP,或 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 以获取大量使用 stubbing 的示例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 服务结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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