The Future of Writing Tests for Jetpack Compose in Android

Hannah Olukoye
Quick Code
Published in
3 min readJul 1, 2022

--

Credits: https://www.monkeyuser.com/

Are you just getting started with TDD for Android? Test-driven development or TDD is one technique to ensure that tests are included with any new code. When using this method, you write the tests for the addition before you write the implementation code. How much and how to test a component are not governed by any strict rules. In this article, I’ll focus on an instance of how to start adopting TDD when developing an Android app with Jetpack compose.

  1. Illustration — Display a list of items on a screen
  2. What to test/verify
  • The list of items doesn't exist
  • The list items exist
  • Check if an item in the list is a text

3. Writing the tests

Luckily, Compose allows us to test components independently. Previously, to display a huge number of items in lists before Jetpack Compose, you used a RecyclerView and an Adapter. Now, you can utilize LazyColumn or LazyRow to display lists either vertically or horizontally respectively. For this example, you will use LazyColumnto display the list of items. Some of the parameters to test include:

    modifier:
state:
contentPadding:
reverseLayout:
verticalArrangement:
horizontalAlignment:
flingBehavior:
userScrollEnabled:
content:

Step 1 — Select the Compose UI content to be used by calling the setContent method of the ComposeTestRule in the test file.

Step 2 — Pick any two/three parameters from the list above and pass fake data that you can control to see if the tests fail/pass.

Here's what you should have so far.

Step 3 — Run the test on Android Studio to get the status.

Step 4 — You can now proceed to verify the conditions mentioned previously. Remember to name your functions after the particular task you are want to test e.g. lazyColumnTestIfListIsNotEmpty

Use this sample code and then run it on Android Studio.

You will notice that lazyColumnTestIfListIsEmpty fails with an error description in the logs (as it should)

Step 5 — One last validation before you go ahead with the implementation code. Say you want to determine whether the first item on the list is a text with a particular value. For this example, assume that you will retrieve data from a fictitious demo data class. You will need to manually add some dummy data. You would then be required to attach this data to your LazyColumn and use an assert verification action to check whether the first value matches a particular string of text.

To make sure your tests are operating as they should, run the code below in Android Studio and modify the text values in the data class.

4. Implementing the actual code

Nice, you got this far! Now you can implement the code for your main screen to display the list of items. It should look something like this.

Remember to declare the list of items for example:

val demo = listOf(
Demo("Item 1"),
Demo("Item 2"),
Demo("Item 3"),
Demo("Item 4")
)

Also, define the data class as:

data class Demo(val description: String)

PS: On Android Studio, you can see the Preview of our list by adding the annotation before the composable function.

For other similar (more advanced) publications on this, check out Paul Allies article on Getting Started with TDD: Kotlin and Jetpack Compose

--

--