The reason I am writing this article is because it was very hard for me to find some unit testing materials on Android Components. I hope I can fill the gap. Let’s talk about unit testing for a bit. I am not going to explain what unit testing is in this article. The official Android documentation about unit testing can be a good place to start. Although, it will be good to talk about some of the advantages of writing unit tests.

  1. It helps us refactor/change old code more confidently without fear of breaking something you don’t know.

  2. It forces you to write better code

  3. It makes debugging easier. If a unit test fails it means your latest change breaks something

  4. Eventually, unit testing leads to less bugs

These are the main reasons why we should write unit tests in our projects. Now let’s talk about how to test Android Components.

Mainly I will try to explain Activity/Fragment unit testing with the new AndroidX Testing library. You can just simply add this dependency to use it

    testImplementation 'androidx.test:core-ktx:{LATEST-VERSION}'

This library introduces us to two new classes ActivityScenario and FragmentScenario. With these two scenario classes, we can launch any activity or fragment and test their methods. You can easily change their states too.

One other benefit of this library is you can run these tests on JVM(using Roboelectric) or on a device. All you need to do is to decide where to run deciding which folder to use test or androidTest . test for JVM, androidTest for device.

These scenario classes have very easy to use API. Let’s check them.

    val scenario = launchActivity<MainActivity>()

    // Runs a given action on the current Activity's main thread.
    scenario.onActivity { activity ->


    // Moves Activity state to a new state

    // Waits for the activity to be finished and returns the activity

    // Finishes the managed activity and cleans up device's state


To write unit tests we need to configure our project a little bit more.

  • Add this option to your project’s Gradle file:
    android {
        testOptions {
            unitTests.includeAndroidResources = true
  • Add more dependencies to write unit tests more efficiently
    testImplementation 'androidx.test.ext:junit-ktx:{LATEST-VERSION}' // Adds AndroidJUnit runner
    testImplementation 'org.robolectric:robolectric:{LATEST-VERSION}' // to run Android tests on JVM
    testImplementation 'io.mockk:mockk:{LATEST-VERSION}' // Kotlin friendly mocking library
    testImplementation 'androidx.test.espresso:espresso-core:{LATEST-VERSION}' // to write android UI tests
  • Add this annotation @RunWith(AndroidJUnit::class) to your test suite.


Let’s start with an example. Let’s assume that we have an activity called MainActivity . Let’s test if it starts without crashing.

    private lateinit var scenario: ActivityScenario<MainActivity>

    fun `Starts without crashing`() {
        scenario = launchActivity()

That’s it. All we need to do is use the launchActivity function. It will create and start the activity for us.

Let’s do a more complicated example. Assume that we have a button and our ViewModel controls if the button is enabled or not.

    private val viewModel: MainViewModel = mockk(relaxUnitFun = true)

    fun `Save button starts enabled`() {
        // Given
        every { viewModel.saveButtonEnabled } returns true

        // When
        scenario = launchActivity()

        // Then

So what happened here?

  1. Mock the ViewModel and define the behavior of saveButtonEnabled flag variable.

  2. Start the activity

  3. Check if the button is enabled or not


What we achieved in this article is to use Google’s new test API and write some small and easy unit tests. AcitivityScenario/FragmentScenenario is definitely the best and easiest way to write unit tests for Android components. I strongly recommend it.

I want to share one more benefit with you. It really feels nice to see all of your tests are passed :)


Note: You can check my sample repo and see the setup and some more complex examples.

Article photo by Bill Oxford on Unsplash


Deniz Tunç Tuncer

Senior Android Developer

Android Engineer, 📱 💻 🚘 enthusiast.

You may also like

NestJS Starter Kit, Monstarlab edition

TL;DR: We built a Node.js backend starter kit based on NestJS Framework. It follows a Monolithic architecture and implements REST APIs.


PicoRuby (f/k/a mmruby) is an alternative Ruby implementation dedicated to one-chip microcontrollers. The binary fits into less than 256 KB ROM and runs on less than 64 KB RAM. This development was supported by the Ruby Association (RA) Grant program 2020 and mentored by Matz himself! Monstarlab is one of...