Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Should I Unit Test WebServiceTemplate (SpringWS)

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?

like image 251
Karthik Ramachandran Avatar asked Jan 31 '11 15:01

Karthik Ramachandran


People also ask

What is unit test assertions?

The assert section ensures that the code behaves as expected. Assertions replace us humans in checking that the software does what it should. They express requirements that the unit under test is expected to meet. Now, often one can write slightly different assertions to capture a given requirement.


1 Answers

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.

like image 159
Karthik Ramachandran Avatar answered Sep 27 '22 20:09

Karthik Ramachandran