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.
MockWebServer
for writing unit 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 :
MVP
architecture inspired by repository.Dagger2
Retrofit2
to fetch from the Pokemon APITestScheduler
and TestObserver
for writing tests.Mockito
for mocking out the View
s and other things for Presenter
testing.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