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
    state.scenario.moveToState(Lifecycle.State.RESUMED)

    // Waits for the activity to be finished and returns the activity
    result.scenario.result

    // Finishes the managed activity and cleans up device's state
    scenario.close()

Preparing

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.

Testing

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>

    @Test
    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)

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

        // When
        scenario = launchActivity()

        // Then
        onView(withId(R.id.saveBtn)).check(matches(isEnabled()))
    }

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

Conclusion

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 :)

conclusion

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

Article photo by Bill Oxford on Unsplash

Author

Deniz Tunç Tuncer

Senior Android Developer

Android Engineer, 📱 💻 🚘 enthusiast.

You may also like

Let it flow

I’ve been working with Flow on production for almost a year now. The light weight kotlin coroutines stream library has completely replaced RxJava for our new projects. RxJava is still a trend for Android Development even in 2020. But this article is not another RxJava x Coroutines. It is here...

Alibaba Seata

Seata is an open source distributed transaction solution that delivers high performance and easy to use distributed transaction services under a microservices architecture.