Mock project showcasing testing on Android.
Find a file
2022-01-23 22:40:28 +02:00
.github Update issue templates 2022-01-04 19:00:13 +02:00
app Issue#11 Adjust SharedTests by using new TestDispatcher instead of deprecated TestDispatchers 2022-01-23 22:40:28 +02:00
codekata Proof read core instruction set 2022-01-22 11:15:18 +02:00
core Issue#11 Adjust ContentRepositoryTest by using runTest correctly instead of deprecated TestDispatcher 2022-01-23 20:29:44 +02:00
detekt Fix codeAnalysis errors 2021-09-18 17:28:53 +03:00
gradle/wrapper Add pipeline: codeAnalysis, tests and clean up gradle 2021-09-18 17:03:28 +03:00
gradlescripts Update core tests to the coroutines tests 1.6.0 2022-01-22 10:55:02 +02:00
mockserver Issue#4 Add scrolling to recyclerview so elements can be verified safely 2022-01-05 14:55:52 +02:00
model Fix code analysis errors 2021-09-19 02:19:01 +03:00
network issue#6 Add DisplayName annotation to ViewModel tests in app module 2022-01-04 14:34:46 +02:00
.gitignore initial commit 2021-04-12 00:32:09 +03:00
build.gradle Issue#4 Add scrolling to recyclerview so elements can be verified safely 2022-01-05 14:55:52 +02:00
gradle.properties PR#28 Update moshi dependency so kotlin update doesn't break build 2022-01-04 18:43:32 +02:00
gradlew initial commit 2021-04-12 00:32:09 +03:00
gradlew.bat initial commit 2021-04-12 00:32:09 +03:00
LICENSE add License generated by GitHub 2021-04-12 00:29:11 +03:00
README.md Update README.md 2021-09-19 02:30:53 +03:00
settings.gradle initial commit 2021-04-12 00:32:09 +03:00

Android Test Showcase

License

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

Preparation

Download the project, open it in Android Studio.

  • In the gradle window you can see in the root gradle there is a "tests" group. In this group you will see a unitTests and androidTests task.
  • First run the unitTests.
  • When that finished, build the application to your phone.
  • Login with whatever credentials and look over the app, what will you test.
  • When finished, run androidTests.

This will ensure the testing setup is proper, the project can resolve all the dependencies and such issues won't come up during your exercise.

If everything is right, change branch to codeKata and look for into the codekata folder for the instruction sets.

Structure

The Code Kata is structured into 5 different section, each section in different what we are testing and how we are testing it.

Since our layering is "app", "core" and "networking", of course we will jump right into the middle and start with core.

Core

Open the core instruction set.

The core tests are the simplest, we will look into how to use mockito to mock class dependencies and write our first simple tests.

Next we will look how to test flows.

Networking

Open the networking instruction set.

The networking instruction set will show you how to test network request with mockwebserver.

It will also show you that you can write tests not only for one class mocking all the dependencies, but a component.

App ViewModel Unit Tests

Open the app viewModel unit tests instruction set.

This section we will see how to replace the dispatcher to testDispatcher to control the ViewModel's coroutines.

We will also see how to test with LiveData.

We will introduce Rules, aka easy to reuse "Before" and "After" components.

App Robolectric Unit Tests.

Open the app storage unit tests instruction set.

In this section we will see how to test component depending on context such as Room database.

Robolectric and Android Tests.

Open the shared tests instruction set.

In this tests we will see how to interact with View components in tests via Espresso.

We will also see how to test a specific Activity (same concept can be applied to fragments)

We will also see how can we share Robolectric test source with AndroidTests to run our same tests on actual device.

License

License file