How Should I Unit Test WebServiceTemplate (SpringWS)

Karthik Ramachandran picture Karthik Ramachandran · Jan 31, 2011 · Viewed 8.2k times · Source

I'm trying to figure out the best way to write unit tests for Spring's WebService Template classes. What I'm trying to do is check to make sure that I wired these classes up correctly for each request/response type that my client is producing. I also want to make sure that if an exception is returned the exception is handled correctly.

I guess what I'm trying to do is figure out a way to make the actual send/receive calls.

Any suggestions?

Answer

Karthik Ramachandran picture Karthik Ramachandran · Jan 31, 2011

I'm going to be tacky and answer my own question.

After a little further research I found that Spring WS 2.0 has a new client testing framework that does exactly what I was hoping to do (from http://blog.springsource.com/2011/01/11/spring-web-services-2-0-released/):

The core class for doing client-side integration testing is the MockWebServiceServer. The underlying idea is that the web service template connects to this mock server, sends it request message, which the mock server then verifies against the registered expectations. If the expectations are met, the mock server then prepares a response message, which is send back to the template.

The typical scenario of testing client-side code consists of:

  1. Creating a MockWebServiceServer.
  2. Setting up expectation(s) about the request message.
  3. Creating an appropriate response message
  4. Using the WebServiceTemplate as normal, either directly of through client code.
  5. Call MockWebServiceServer.verify() to make sure that all expectations have been met.

Now unfortunately my project still uses spring-ws 1.5.9. I'm going to try and upgrade just the client to 2.0 and see if anything breaks. If that goes well I might try switching over the server side soon.