How can I unit test code sending a SOAP web service request?

I want to write 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 factory is javax.xml.soap.SOAPConnectionFactory

I do not need an answer, but I want to check the sent message. The code will be reorganized, and I want it to send the same messages later, as before.

Is it possible to create a framework that I can use to create a layout endpoint that will allow me to parse the request in my test? If so, some sample code will be very helpful.

+4
source share
2 answers

There is a java.net project called WSUnit that should help. It is basically a listener servlet that listens for messages. You can send messages to him and check the contents of the message using XMLUnit.

+4
source

Use JMock . JMock allows you to check behavior, not state changes. To do this, you need to encapsulate the SOAPCOnnectionFactory.newInstance () method in another object:

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

Use the object of the new class in your code.

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

Then in your test, replace the Mock object for Factory, which will return the Mock connection. Set expectations in Mock Connection, waiting for the call you are 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); } } ); 
+2
source

All Articles