

Instead of dealing with an actual server's response which changes, it's better to deal with a mocked response that can be controlled. After running the test case, it failed, because the text field's data was not matching the expected data as API response keeps changing.Īnd here comes MockWebServer to the rescue.

To test the displayed data, we wrote a test case with the help of Espresso. Let's create a demo application, which displays data from two different Star Wars APIs.
#Mockwebserver enqueue multiple how to
So we thought to demonstrate how to set up a mock server using the MockWebServer library and how we overcame the challenge of testing async API calls. It also handles the uncertain sequence of API calls flawlessly. After exploring a lot of libraries, we found the MockWebServer library that helps in mocking a real server & writing API test cases. For obvious reasons, it's not a good practice to call the actual API for test cases. To achieve maximum code coverage, we decided to write test cases that include API calls. Most of them were nested or getting called at the same time. Recently we were working on an Android application that was full of API calls. For unit & integration testing, Android supports multiple frameworks. Nowadays, writing test cases for every feature of your app has become inevitable! It ensures the app’s correctness, behaviour, and usability at any given moment.

3 min read How To Test API Response With MockWebServer Library The Right Way.
