Testing Application Interaction with Unavailable Web Service – Mock Servers

While the applications are being developed, there are several states when the dependencies are not yet available. But the applications are built with certain assumptions (or published design) about the dependent third party behavior. It is very common in today’s micro service based architecture where interfacing micro services are being developed in parallel and testing requires the other service to be available. It is preferable to have the application tested during the development phase itself so as to eliminate the obvious failure scenarios and tackle unexpected responses.

If the developer is familiar with writing unit tests, such dependencies are generally met using mock objects. Synonymous equivalent of mock objects in end to end testing are mock servers. Mock servers are those dummies which are programmed to behave like a real application in order to achieve the timely testing of a given application when the dependency is still unavailable.

Mock servers are built based on the available specification of the third party which is consumed by the system under development / test. Apart from the discussed, there can be other scenarios where mock servers can come handy – for execution of unit / integration tests. Mock servers in these cases help by reducing the time taken to execute tests as a mock server can be run locally on the machine where the tests are run. This also eliminates the test failures due to false positives when dependent applications / services are down.

For the use cases discussed, below are the desirable qualities of mock servers:

  1. Easy to set-up / tear-down.
  2. Easy to implement / configure.
  3. Shorter learning curve.
  4. Lightweight and fast.

There are many solutions available in the market that provide a suite of features required to mock any service and ease the process of development / testing. Listing here the ones which are easy to pick up and solve the discussed problems for a Java developer. There are many alternatives and can match varied requirements across the projects.

1. Development phase – Mockoon

During the development phase, we do not really need to keep the server running, but should be able to tweak the server as and when needed. Should be fast and easily customizable as and when needed. Mockoon is a GUI based mock server which can, at a time, run multiple mock servers. Mocks are easy to configure in the UI and server starts up really fast without wasting the time during development.

2. Unit Testing / Integration Testing – WireMock

WireMock is a very useful tool that can greatly help in unit testing as well as integration testing. This has too many features and requires some time and effort to understand its entire capabilities. For unit testing, one can make use of the Junit Rule (for Junit4, or manually initialize for Junit 5) and verify the invocations post execution. Details are available in the getting started guide.

For integration tests, it comes with record and playback feature where the requests and responses can be learnt while running the application against the real dependencies and run in playback mode during the execution of tests.