如何对发送 SOAP Web 服务请求的代码进行单元测试?
我想为一些代码编写一个单元测试,生成带有附件的 SOAP 消息并发送它:
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
conn = factory.createConnection();
conn.call(message, endpoint);
其中工厂是 javax.xml.soap.SOAPConnectionFactory
我不关心任何响应返回,但我希望能够验证发送的消息。代码将被重构,我想确保它之后发送与以前相同的消息。
是否有一个框架可以用来创建模拟端点,让我可以在测试中分析请求?如果是这样,一些示例代码将非常有用。
I want to write a unit test for some code that generates a SOAP message with an attachment and sends it:
SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance();
conn = factory.createConnection();
conn.call(message, endpoint);
where the factory is a javax.xml.soap.SOAPConnectionFactory
I don't care about any response coming back but I want to be able to verify the message that gets sent. The code is going to be refactored and I want to make sure it sends that same messages afterwards as before.
Is there a framework i can use to create a mock endpoint that will let me analyse the request in my test? If so, some sample code would be very useful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个名为 WSUnit 的 java.net 项目应该会有所帮助。它基本上是一个侦听消息的侦听器 servlet。您可以向其发送消息并使用 XMLUnit 验证消息的内容。
There is a java.net project called WSUnit that should help. It is basically a listener servlet which listens for messages. You can send messages to it and verify the content of the message using XMLUnit.
使用 JMock。 JMock 允许您测试行为而不是状态更改。为此,您需要将 SOAPCOnnectionFactory.newInstance() 方法封装在不同的对象中:
在代码中使用新类的对象。
然后,在您的测试中,用模拟对象替换工厂,它将返回模拟连接。对模拟连接设置期望,以期待您正在寻找的呼叫。
Use JMock. JMock allows you to test for behavior rather than state changes. To do this, you'll need to encapsulate the SOAPCOnnectionFactory.newInstance() method in a diffent object:
Use an object of the new class in your code.
Then, in your test, substitute a Mock object for the Factory, which will return a Mock Connection. Set expectations on the Mock Connection expecting the call you're looking for.