restmockingstubstubbingstubby4j

How to stub some requests and call real service for others using stubby4j


I am using stubby4j to stub some service endpoints. I am currently stubbing the ones that are very heavy and not so complex to mock but I would like to call the real service for the rest of the endpoints.

Something like this:

/heavy-call-1 => stub service
/heavy-call-2 => stub service
/lightweight-call-1 => real service
/lightweight-call-2 => real service

Is there a way I can achieve this with this tool or should I consider using a different one?


Solution

  • You can actually make stubby call the real service and record the response for the first time, so the next requests will use this recorded response. The way you can do this is by specifying an URL in the body of the stubbed response in your yaml file like this:

    -  request:
          url: /1.1/direct_messages.json
          query:
             since_id: 240136858829479935
             count: 1
       response:
          headers:
             content-type: application/json
          body: https://api.twitter.com/1.1/direct_messages.json?since_id=240136858829479935&count=1
    

    You can find some more information in the stubby github docs: https://stubby4j.com/#key-features and https://stubby4j.com/docs/http_endpoint_configuration_howto.html#record-and-replay

    Hope this helps!