在 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]

  1. 我需要使用 MockWire API 吗?我需要为我的用例创建类吗?在我的例子中,请求将通过 JUnit 测试用例触发以进行模拟。

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

  1. 我的用例是否有更好/更简单的框架?我阅读了有关 Mockable 的信息,但它对 3 名团队成员和免费层中的演示域有限制。

原文由 Anurag 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 800
2 个回答

我是 WireMock 的创建者。

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

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

依次解决您的观点:1)如果您希望您的模拟在某处的服务器上运行,最简单的方法是按照您所描述的那样运行独立的 JAR。我建议不要尝试将它部署到容器中——这个选项实际上只存在于别无选择的情况下。

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

  1. 您需要通过以下 3 种方式之一对其进行配置:1) Java API,2) JSON over HTTP,或 3) JSON 文件。 3) 可能最接近您习惯使用的 SOAP UI。

  2. 请参阅 http://wiremock.org/stubbing.html 以获取大量同时使用 JSON 和 Java 的存根示例。由于 SOAP 倾向于绑定到固定端点 URL,您可能需要 urlEqualTo(...) 。当我过去对 SOAP 进行存根时,我倾向于对整个请求主体进行 XML 匹配(请参阅 http://wiremock.org/stubbing.html#xml-body-matching )。我建议投资编写一些 Java 构建器来发出您需要的请求和响应主体 XML。

  3. Mock ServerBetamax 都是 WireMock 的成熟替代品,但据我所知,它们不提供任何更明确的 SOAP 支持。

原文由 Tom 发布,翻译遵循 CC BY-SA 3.0 许可协议

我迟到了三年多,但我花了一段时间来解决同样的问题,所以我虽然值得记录我的解决方案作为答案,这样它可能会让其他人免于手动处理 SOAP 有效负载的麻烦刮。

我做了一些合理的研究,试图为我的集成测试套件解决这个问题。尝试了各种各样的东西,包括 CXF 自定义生成的服务器、SOAP-UI、一个受 CGLIB 影响的库,它在测试上下文中替换了真实的客户端。

我最终使用带有 自定义请求匹配器 的 WireMock 来处理所有 SOAP -yness。

它的要点是一个处理 SOAP 请求的解组和 SOAP 响应的编组的类,以便为测试作者提供一个方便的包装器,测试作者只需要 JAXB 生成的对象,而不必关心 SOAP 的细节。

响应编组

/**
 * Accepts a WebService response object (as defined in the WSDL) and marshals
 * to a SOAP envelope String.
 */
public <T> String serializeObject(T object) {
    ByteArrayOutputStream byteArrayOutputStream;
    Class clazz = object.getClass();
    String responseRootTag = StringUtils.uncapitalize(clazz.getSimpleName());
    QName payloadName = new QName("your_namespace_URI", responseRootTag, "namespace_prefix");

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
        Marshaller objectMarshaller = jaxbContext.createMarshaller();

        JAXBElement<T> jaxbElement = new JAXBElement<>(payloadName, clazz, null, object);
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        objectMarshaller.marshal(jaxbElement, document);

        SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();
        SOAPBody body = soapMessage.getSOAPPart().getEnvelope().getBody();
        body.addDocument(document);

        byteArrayOutputStream = new ByteArrayOutputStream();
        soapMessage.saveChanges();
        soapMessage.writeTo(byteArrayOutputStream);
    } catch (Exception e) {
        throw new RuntimeException(String.format("Exception trying to serialize [%s] to a SOAP envelope", object), e);
    }

    return byteArrayOutputStream.toString();
}

请求解组

/**
 * Accepts a WebService request object (as defined in the WSDL) and unmarshals
 * to the supplied type.
 */
public <T> T deserializeSoapRequest(String soapRequest, Class<T> clazz) {

    XMLInputFactory xif = XMLInputFactory.newFactory();
    JAXBElement<T> jb;
    try {
        XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(soapRequest));

        // Advance the tag iterator to the tag after Body, eg the start of the SOAP payload object
        do {
            xsr.nextTag();
        } while(!xsr.getLocalName().equals("Body"));
        xsr.nextTag();

        JAXBContext jc = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        jb = unmarshaller.unmarshal(xsr, clazz);
        xsr.close();
    } catch (Exception e) {
        throw new RuntimeException(String.format("Unable to deserialize request to type: %s. Request \n %s", clazz, soapRequest), e);
    }

    return jb.getValue();
}

private XPath getXPathFactory() {

    Map<String, String> namespaceUris = new HashMap<>();
    namespaceUris.put("xml", XMLConstants.XML_NS_URI);
    namespaceUris.put("soap", "http://schemas.xmlsoap.org/soap/envelope/");
    // Add additional namespaces to this map

    XPath xpath = XPathFactory.newInstance().newXPath();

    xpath.setNamespaceContext(new NamespaceContext() {
        public String getNamespaceURI(String prefix) {
            if (namespaceUris.containsKey(prefix)) {
                return namespaceUris.get(prefix);
            } else {
                return XMLConstants.NULL_NS_URI;
            }
        }

        public String getPrefix(String uri) {
            throw new UnsupportedOperationException();
        }

        public Iterator getPrefixes(String uri) {
            throw new UnsupportedOperationException();
        }
    });

    return xpath;
}

除此之外,还有一些 XPath 实用程序,用于查看请求负载并查看请求的操作。

所有 SOAP 处理都是开始工作中最繁琐的部分。从那里它只是创建您自己的 API 来补充 WireMocks。例如

public <T> void stubOperation(String operation, Class<T> clazz, Predicate<T> predicate, Object response) {
    wireMock.stubFor(requestMatching(
                     new SoapObjectMatcher<>(context, clazz, operation, predicate))
                    .willReturn(aResponse()
                    .withHeader("Content-Type", "text/xml")
                    .withBody(serializeObject(response))));
}

结果你得到了一个很好的、精简的测试。

 SoapContext context = new SoapContext(...) // URIs, QName, Prefix, ect
context.stubOperation("createUser", CreateUser.class, (u) -> "myUser".equals(u.getUserName()), new CreateUserResponse());

soapClient.createUser("myUser");

原文由 markdsievers 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题