Junit

In AndroidPub. More on Medium.

We all know the best practices about developing with Android. It is about using Kotlin, Dagger 2, Retrofit and RxAndroid. But when your architecture is done, the next question is how to write good unit tests.

The project we will work on

We will work on an MVP project with Kotlin, Dagger 2, Retrofit and RxAndroid which is based on JSONPlaceholder API to display a list of posts. It currently has no unit test at all.

The source code can be found on the annotationprocessor branch of the Feed-Me project on GitHub.

Setting up unit tests

Once you get the project from GitHub, add a class named in the…


There comes a point in your software developer journey where you realise that unit testing is really important. This especially happens if you want to make sure that your code still works after you do some changes.

In Android, there are two types of tests:

  1. JUnit Tests
  2. Android instrumented unit test

This article focuses on the latter. Espresso testing framework is usually used to automate UI testing with the help of AndroidJUnitRunner test runner. Other libraries also exist such Robolectric.

Firstly, assuming that you have an android project ready, add the below extra gradle dependencies:

Testing Grardle Dependencies

Gradle will…


Source: Google

Why do we need?

Have you ever felt boring when you have to run your app for a small little change to see it is working fine or not?

If yes then, you must try Robolectric, it is a unit testing framework that allows Android applications to be tested on the JVM without an emulator or device.

These are the main features of it:

  1. It provides a way to run our tests inside Android Studio, without launching app on Device or Emulator.
  2. Shadow Classes, rewritten android core libraries by Robolectric, are the real magic of Robolectric. It is a replica of Android class with…

AndroidPub

The (retired) Pub(lication) for Android & Tech, focused on Development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store