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.
Most of the demos out there make use of MockWebServer
for writing androidTest
s and adding custom JUnitTestRunner
s 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 Presenter
s / 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 :
- Uses RxJava for the demos.
- Follows the
MVP
architecture inspired by repository. - Makes use of
Dagger2
- Uses
Retrofit2
to fetch from the Pokemon API - Uses Butterknife
- Uses
TestScheduler
andTestObserver
for writing tests. - Also uses
Mockito
for mocking out theView
s and other things forPresenter
testing. - This project makes use of
MockWebServer
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 Mockito
to 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
- More Documentation.