如何对发送 SOAP Web 服务请求的代码进行单元测试?

发布于 2024-09-18 18:17:18 字数 384 浏览 3 评论 0原文

我想为一些代码编写一个单元测试,生成带有附件的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜夜流光相皎洁 2024-09-25 18:17:18

有一个名为 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.

半仙 2024-09-25 18:17:18

使用 JMock。 JMock 允许您测试行为而不是状态更改。为此,您需要将 SOAPCOnnectionFactory.newInstance() 方法封装在不同的对象中:

public class MySOAPConnectionFactory {

    public SOAPConnectionFactory getConnection() {
        return SOAPConnectionFactory.newInstance();
}

在代码中使用新类的对象。

conn = mySOAPConnectionFactory.getConnection();
conn.call( message, endpoint );

然后,在您的测试中,用模拟对象替换工厂,它将返回模拟连接。对模拟连接设置期望,以期待您正在寻找的呼叫。

final SOAPConnectionFactory mockConnection = mockery.mock(SOAPConnectionFactory.class);
final SOAPConnection mockSoapConnection = mockery.mock(SOAPConnection.class);
foo.setMySOAPConnectionFactory( mockConnectionFactory );
try {
        mockery.checking( new Expectations() {

        {
            atLeast( 1 ).of( mockConnectionFactory ).getConnection();
            will( returnValue( mockSoapConnection ) );
            atLeast( 1 ).of( mockConnection ).call(SOME_MESSAGE, ENDPOINT);
        }
        } );

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:

public class MySOAPConnectionFactory {

    public SOAPConnectionFactory getConnection() {
        return SOAPConnectionFactory.newInstance();
}

Use an object of the new class in your code.

conn = mySOAPConnectionFactory.getConnection();
conn.call( message, endpoint );

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.

final SOAPConnectionFactory mockConnection = mockery.mock(SOAPConnectionFactory.class);
final SOAPConnection mockSoapConnection = mockery.mock(SOAPConnection.class);
foo.setMySOAPConnectionFactory( mockConnectionFactory );
try {
        mockery.checking( new Expectations() {

        {
            atLeast( 1 ).of( mockConnectionFactory ).getConnection();
            will( returnValue( mockSoapConnection ) );
            atLeast( 1 ).of( mockConnection ).call(SOME_MESSAGE, ENDPOINT);
        }
        } );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文