Demo project on How to be a Mockstar using Mockito and MockWebServer.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

mockstar

Demo project for using MockWebServer for unit tests. This project also hints on mocking out views while testing using Mockito and focuses on the usage of MockWebServer for writing tests.

Using MockWebServer for writing unit tests.

Most of the demos out there make use of MockWebServer for writing androidTests and adding custom JUnitTestRunners and are more focused on integration testing. Nowadays, MVP architecture is getting adopted by many android developers and we like our logic layer to be tested properly. MVP enables proper testing of the logic layer via unit tests and separates it from the pains of testing the android UI. While testing, we should not hit the actual web server. If we hit actual server while testing, then the tests become flaky as they depend upon the actual network availability. This project shows how we can write unit tests for our Presenters / Models easily. It also shows how we can test certain error conditions properly with the use of Mockito and MockWebServer which becomes as easy as just hitting the actual remote server.

Project implements the following :

Implementation

This project has a separate module called mocks. Our android application module app has a testCompile dependency on the mocks module. Also, created a LocalResponseDispatcher which takes care of the local API needs.

We use MockWebserver to test the API calls in our ModelRepository (PokemonDataSource) and we use Mockitoto test our Presenter (MainPresenterImpl) by mocking out the view. We have also written some demo tests using MockWebServer just to show how same thing can be achieved using MockWebServer also.

Model - PokemonDataSource -> PokemonDataSourceTest (uses MockWebServer) Presenter - MainPresenterImpl -> MainPresenterImplTest (uses MockWebServer) -> MainPresenterImplMockTest (uses Mockito)

To run tests : ./gradlew clean test

To-Do

  • More Documentation.