問題描述
我對 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 這樣的工具可以讓您更快地開始,但從長遠來看,當您的測試套件變得非?,嵥闀r,往往會導致更高的維護成本.
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模板網!