Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to mock java web service [closed]

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 asynchronous 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)?

like image 517
aaimnr Avatar asked Jun 25 '09 08:06

aaimnr


People also ask

What is stubbing Java?

Mocks and stubs are fake Java classes that replace these external dependencies. These fake classes are then instructed before the test starts to behave as you expect. More specifically: A stub is a fake class that comes with preprogrammed return values.

What is@ mock in Java?

A Mock object is something used for unit testing. If you have an object whose methods you want to test, and those methods depend on some other object, you create a mock of the dependency rather than an actual instance of that dependency.


3 Answers

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

like image 185
alightgoesout Avatar answered Sep 27 '22 23:09

alightgoesout


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/).

like image 35
Nick Holt Avatar answered Sep 27 '22 22:09

Nick Holt


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.

like image 28
Michał Niklas Avatar answered Sep 28 '22 00:09

Michał Niklas