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

Engineering Awesome Conference 2020

We love engaging with the tech community, and we are big consumers of the awesome work that people share online. So a couple of weeks ago we hosted our first-ever online conference as a way to give back to the community. 6 experts from across our offices in Europe and...

API First

We’ve been building APIs for a long time, enabling our mobile software solutions to communicate with each other across their respective platforms. For years these APIs have been built as part of a “mobile first”-approach, under the guiding light of an internally defined, living document, our so called “API Manifesto”,...