views:

1750

answers:

3

I have to mock quite complicated java web service and I'm searching for the right solution. One way to do it would be to use Soap UI, but I need something that would be able to modify server state ie. one request would affect future requests.

In this particular case it could be done quickly by saving serialized objects to disk and sometimes spawning asynchronouse responses to the originating client webservice.

Those two requirements are preventing me from using SoapUI - the groovy logic would become quite complicated and probably hard to mantain.

My questions:

1) Are there any other SoapUI advantages in this context (eg. easy migration to new version of wsdl) over custom java mock implementation?

2) What would be most appropriate way to generate the webservice from wsdl and still be able too hook up with some custom functionality, ie. by attaching some hooks that would be editable in seperate files (to facilitate further ws code regeneration from updated wsdl)?

+4  A: 

You should look at EasyMock, which allows to build mocks programatically. It is possible to specify very complex behaviors for your mocks.

Romain
+2  A: 

Presumably you're using some sort of generated stub in your client? You should mock stub with one of the mocking APIs (JMock or EasyMock) and inject the mock into the class-under-test.

On the server-side test that class that handles the call, injecting mocks of any objects it might use to do its job.

As an aside you should strive to keep all the calls in a unit test local (in-process). It makes it easy to control return values from any objects the class-under-test depends on and when the test suite grows will help prevent the unit tests becoming a bottle neck in your build process.

With regards to generating a Java class(es) from WSDL Apache Axis has something called WSDL2Java, which generates the client stubs I mentioned earlier. This sort of utility is common in the web service frameworks but may have been replaced now since EJB3 web services introduced javax.xml.rpc.ServiceFactory exists.

There's a tutorial on EJB3 web services and clients here (http://www.theregister.co.uk/2007/01/23/ejb_web_services/).

Nick Holt
+1  A: 

For simple mocks I use soapUI, while for more complicated when state must change between request I use simple web service emulator written in Python. Such emulator use reply templates created from real web service or responses I created in soapUI. This way I can control all logic.

Emulator for my last project has 300+ lines of Python code, but for previous, much simplier, it was ~150 lines of Python code.

Michał Niklas