Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The power of mocking APIs

Avatar for Shivani Gaba Shivani Gaba
September 01, 2020
520

The power of mocking APIs

Blocked because the API you depend on doesn’t exist yet or isn’t completely ready? Facing trouble to test various scenarios due to lack of control over third-party APIs? Struggling to test failure cases like receiving an invalid response , 5XX errors and so on? Having flaky tests due to slow API responses?

These are some very common problems we encounter. We cannot rely on slow APIs, which provide a very narrow range of responses. So how can we test effectively in such situations? Is there any feasible solution available? Fortunately, there is: stubbing the APIs.

This presentation is about creating a mock server and stub responses to get rid of other dependencies.

Below are links used for demo in the presentation

Weather application source code-
https://github.com/ShivaniGaba1/weather-app/

Wiremock project code-
https://github.com/ShivaniGaba1/wiremock-weather/

Avatar for Shivani Gaba

Shivani Gaba

September 01, 2020
Tweet

Transcript

  1. • Easy to setup and configure • Offer various features

    • Can be stand-alone or embedded • Active community supports Wiremock Approach over tools @shivani_gaba_
  2. Similar ideas - • Set any response body • Complicated

    Cases • Edge cases @shivani_gaba_
  3. Similar ideas - • Body - Corrupt format - Missing

    mandatory fields • Headers - Missing headers - Invalid headers …. @shivani_gaba_
  4. Similar ideas- • Any status codes - Server side 5XX

    (ISE, service unavaible etc) - Client error 4XX (bad request, rate limiting etc) - Non-error codes • Faults - Connection_reset, random_data_close etc. @shivani_gaba_
  5. Pros • Test in isolation • Reduce dependencies • Parallel

    development • Realiable • Fast response • Test complicated/edge cases • Test delays • Prevents rate limiting Cons • Maintainance • Complexity increase @shivani_gaba_
  6. Client @Rule public WireMockRule wireMockRule = new WireMockRule(int port); wireMockRule.start();

    public void stubWithServiceUnavailable() { /** Stub with service unavailable**/ stubFor(get(urlEqualTo("/getSomething”)) .willReturn(aResponse() .withStatus(503))); } If application is in same framework, then- @shivani_gaba_
  7. Summary • How & why to setup mock servers •

    Stub responses • Test in isolation • Different levels of testing • Check weather @shivani_gaba_