Tuesday, May 8, 2012

What is an easy way to stub / dummy a restful web service?


I want to create an android application, this application will make RESTful calls to a web service to obtain some data.



I know what the RESTful interface will be, but I don't want the hassle of creating my own implementation. Is there an easy way to create a stub RESTful web service that will return some static data without having to write a full blown WS application to do this?


Source: Tips4all

4 comments:

  1. Probably the best thing to do is create a mock for the REST web service service while you're developing your application code and then replace it with code to call the actual web service returning "real" data, once your application is written.

    I'm currently writing a very similar application to yours which (like you) obtains data from a RESTful web application. In my application, I'm following the MVP pattern recommended by GWT and is also documented by Martin Fowler as the PassiveView pattern.

    What you want to do is abstract away the code to make the REST web service call into an interface (the Model). The responsibility of this model class is to provide data to the Presenter/Controller. The Presenter will handle all of your business logic and then pass data up to the view (the view should be pretty dumb as well allowing it to also be mocked out). During testing, you will create a MockModel to implement the model interface and pass test data to the Presenter - without making an actual web service call at all! Then, when you're ready, you will replace this class with the actual web service and start your integration testing.

    This approach has the added benefit in that it will be easy to create specific (and repeatable) test cases in your mock model. If you don't have control of the actual web service (and I'm assuming you don't), this can be difficult (or even to impossible) to achieve. The result should be a more robust, better tested application without to need to create any test XML or JSON or creating the web services yourself.

    ReplyDelete
  2. I've found using Sinatra really useful for this sort of thing if you want to test the actual HTTP calling code. You can have a endpoint returning data in seconds. Very little Ruby knowledge required.

    require 'sinatra'
    require 'json'

    get '/Person' do
    content_type :json
    { :id => 345, :key2 => 'John Doe' }.to_json
    end


    Is all you would need to return a simple json object.

    ReplyDelete
  3. You can make use of http://maqueapp.com/ to create the mock web service. Its quick and easy. I heard about it on theflexshow episode 157 (not flexshow!)

    ReplyDelete
  4. Here is the most simple online web service that you can use for testing.

    http://services.aonaware.com/DictService/DictService.asmx?op=Define

    It supports all major protocols.


    SOAP 1.1
    SOAP 1.2
    HTTP GET
    HTTP POST

    ReplyDelete