| app | ||
| core | ||
| detekt | ||
| gradle/wrapper | ||
| gradlescripts | ||
| mockserver | ||
| model | ||
| network | ||
| .gitignore | ||
| build.gradle | ||
| gradle.properties | ||
| gradlew | ||
| gradlew.bat | ||
| LICENSE | ||
| README.md | ||
| settings.gradle | ||
Android Test Showcase
Mock project showcasing testing on Android. I plan to use this as a CodeKata to experiment with testing.
Disclaimer: Every test you see in this project you should take with a pinch of salt, this is my self-taught experimentation of Testing on Android
Project Overview
The project uses mock api and is really simplified. It's used just to showcase example features I usually encounter on Android.
You can login with any credentials, mark favourite content items, log out and that's about it.
As dependency injection / Service Locator koin is used.
The application is separated into different modules each module is tested differently.
Modules
Model
Self explanatory, contains all shared data classes (POJOs) and Exceptions used by the other modules.
There is nothing to test here.
App
The android module of the application, contains the UI (Activities), Room-Database and ViewModels.
The UI is strucured in MVVM.
Has dependency on the core module.
There are 3 kinds of tests in this module:
- junit5 tests for ViewModels
- Robolectric Test for Room Database
- Shared Tests for Screens (shared between Robolectric and AndroidTests)
Unit tests
Verify the ViewModels are reacting to the mocked UseCases properly.
Kotlin-Mockito is used to mock out UseCases comming from the core module.
Koin-Test is used to verify the ServiceLocator modules.
LiveData-Testing is used to verify the LiveData values of ViewModels.
Robolectric
Verifies the DataBase interactions and also verifies the interactions on each Screen.
Robolectric website link.
In Unit and Robolectric tests coroutine-test is used to switch out the mainThread thus enabling fine control over interactions.
AndroidTest
Verifies the interactions with the Screens.
In Robolectric and AndroidTests Espresso is used to interact with UI.
In Robolectric and AndroidTests OkhttpIdlingResources is used to synchronize OkHttp with Espresso.
In Robolectric and AndroidTests Mock Server module is used to mock the network requests.
In Robolectric and AndroidTests InstantTaskExecutor is used to set LiveData values immediately and a Custom implementation in Unit tests.
Core
Business layer of the application. Contains Repositories and UseCases.
Has dependency on the network module. Database/SharedPreferences LocalStorage classes are injected into the core module.
All tests are junit5 and they are using Kotlin-Mockito to mock all dependencies.
Coroutine-test is also used in every test.
The tests are verifying the interactions between the RemoteSource and LocalSource components. It also verifies that ErrorHandling is done properly.
Networking
As the name suggests this is the module which sends requests to the Backend and parses the responses received. All responses and requests are mapped to and from models from the model data classes.
Retrofit + OkHttp + Moshi is used to send and parse requests.
All tests are Junit5. The Retrofit Services are injected into tests to make sure the parsing and other setups are proper.
The tests are verifying all the requests contain the correct arguments, headers, path, methods. It is also responsible to verify the responses are parsed properly.
Mock Server module is used to mock the network requests.
JsonAssert is used to compare JSON models.
Mock Server
This module is not actually part of the APK. This module is only used to unify mocking of Network request between Instrumentation tests from app module and network module.
It contains a way to setup responses to requests in a unified way. Contains all Response.json and expected Request.json files.
MockWebServer is used to respond to the requests sent by OkHttp.
JsonAssert is used to compare JSON models.
OkHttp-TLS is used to have HTTPS requests on Android Tests.
Server
The actual server when running the application is mockapi.io so don't expect actual functionalities in the application.
Code Kata
TODO
The kata will be in a separate branch with instructions and empty tests to be filled.
Future Plans for Myself
This just contains todos for myself when next time I am exercising testing on this project
- Finish Code Kata section, by adding new branch with empty tests and instructions
- Add Room migration tests
- Update RecyclerView tests with Espresso.onData
- Add NavController functionality and tests to it
- Update test names with DisplayName annotation