Color Mode


    Language

Android Unit Testing is Easy

July 15, 2020

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](/assets/img/articles/2020-07-15-android-unit-testing-is-easy/conclusion.webp

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

Article photo by Bill Oxford on Unsplash

androidunit-teststaticblogengineering

Author

Deniz Tunç Tuncer

Deniz Tunç Tuncer

Senior Android Developer

Android Engineer, 📱 💻 🚘 enthusiast.

You may also like

November 7, 2024

Introducing Shorebird, code push service for Flutter apps

Update Flutter apps without store review What is Shorebird? Shorebird is a service that allows Flutter apps to be updated directly at runtime. Removing the need to build and submit a new app version to Apple Store Connect or Play Console for review for ev...

Christofer Henriksson

Christofer Henriksson

Flutter

May 27, 2024

Introducing UCL Max AltPlay, a turn-by-turn real-time Football simulation

At this year's MonstarHacks, our goal was to elevate the sports experience to the next level with cutting-edge AI and machine learning technologies. With that in mind, we designed a unique solution for football fans that will open up new dimensions for wa...

Rayhan NabiRokon UddinArman Morshed

Rayhan Nabi, Rokon Uddin, Arman Morshed

MonstarHacks

ServicesCasesAbout Us
CareersThought LeadershipContact
© 2022 Monstarlab
Information Security PolicyPrivacy PolicyTerms of Service