Reader small image

You're reading from  Mastering Kotlin for Android 14

Product typeBook
Published inApr 2024
Reading LevelIntermediate
PublisherPackt
ISBN-139781837631711
Edition1st Edition
Languages
Right arrow
Author (1)
Harun Wangereka
Harun Wangereka
author image
Harun Wangereka

Harun Wangereka is a Google Developer Expert for Android and an Android engineer with over seven years of experience and currently working at Apollo Agriculture. Harun is passionate about Android development, never tired of learning, building the tech community, and helping other developers upscale their skills. He is a co-organizer at Droidcon Kenya and a former Kotlin Kenya and Android254 co-organizer. Through these communities, he has been able to impact thousands of developers. He is also an Android author at Kodeco where he has written 8 articles, published a book; Saving Data on Android, Second Edition, and is also a video course instructor. He has given numerous sessions on Android and Kotlin across different communities worldwide.
Read more about Harun Wangereka

Right arrow

Testing Your App

Testing Android apps is a crucial aspect of the development process, ensuring that our application functions as intended and meets user expectations. It helps us identify and fix bugs before they reach production and ensure that our app is stable and performs well. This chapter will equip you with the skills to write tests for the different layers of our app that we’ve created so far.

In this chapter, we will learn how to add tests for the different layers in our MVVM (Model-View-ViewModel) architecture. We will learn the importance of adding tests to our apps and how to add unit tests, integration tests, and instrumentation tests.

In this chapter, we’re going to cover the following main topics:

  • Importance of testing
  • Testing the network and database layers
  • Testing our ViewModels
  • Adding UI tests to our composables

Technical requirements

To follow the instructions in this chapter, you will need to have Android Studio Hedgehog or later (https://developer.android.com/studio) downloaded.

You can use the previous chapter’s code to follow the instructions in this chapter. You can find the code for this chapter at https://github.com/PacktPublishing/Mastering-Kotlin-for-Android/tree/main/chaptertwelve.

Importance of testing

Writing tests is a crucial aspect of app development. It has the following benefits:

  • It helps us to identify and fix bugs before they reach production. When we write tests for our code, we can see issues at an early stage and quickly fix them before they reach our users, which is normally very costly.
  • Ensures code quality. When we write tests, we are forced to write code that can be tested. This means that we write code that is modular and loosely coupled. This makes our code base more maintainable and easier to work with. When we find a piece of code that is hard to test, it is a sign that the code is not well written and needs to be refactored.
  • Writing tests results in improved documentation and code understanding. When we write tests, we are forced to think about how our code works and how it should be used. This makes it easier for other developers to understand our code. While tests can serve as a form of documentation, they should not replace...

Testing the network and database layers

In this section, we are going to learn how to write tests for our network and database layers step by step.

Testing the network layer

To test our network layer, we will write unit tests. However, since we are using Retrofit to make our network requests, we will use MockWebServer to mock our network requests. The MockWebServer is a library that allows us to mock our network requests. Let us start by setting up the test dependencies in our version catalog:

  1. Open the libs.version.toml file and add the following versions in the versions section:
    mockWebServer = "5.0.0-alpha.2"
    coroutinesTest = "1.7.3"
    truth = "1.1.5"

    We are setting up the versions for mockWebServer, coroutinesTest, and truth.

  2. Next, in the libraries section, add the following:
    test-mock-webserver = { module = "com.squareup.okhttp3:mockwebserver", version.ref = "mockWebServer" }
    test-coroutines = { module = "org.jetbrains...

Testing our ViewModels

Our ViewModel class fetches data from the repository and exposes it to the UI. To test our ViewModel, we will write unit tests. Let us start by setting up the test dependencies in our version catalog:

  1. Open the libs.version.toml file and add the following versions in the versions section:
    mockk = "1.13.3"
  2. Next, in the libraries section, add the following:
    test-mockk = { module = "io.mockk:mockk", version.ref = "mockk" }
  3. Add the test-mockk dependency to the test bundle. Our updated test bundle should now look like this:
    test = ["test-mock-webserver", "test-coroutines", "test-truth", "test-mockk"]
  4. Click on the Sync Now button at the top to add the dependencies. Adding mockk allows us to mock our dependencies.
  5. We are now ready to create our test class. Create a new Kotlin file called CatsViewModelTest.kt inside the test directory and add the following code:
    class PetsViewModelTest...

Adding UI tests to our composables

Writing UI tests has been made easier for us. Jetpack Compose provides a set of testing APIs to find elements, verify their attributes, and perform actions on these elements. Jetpack Compose uses semantics to identify elements in our composables. Semantics are a way to describe the UI elements in our composables. Semantics are used by accessibility services to make our apps accessible. Testing dependencies are already set up as we created our new project in Chapter 2 so no need to add any dependencies. We have a number of composables on our project. For this chapter, we will write tests for the PetListItem composable.

Let us head over to the PetListItem.kt file. We need to add a testTags modifier to our composable. This is because we are using tags to identify our composables. In the PetListItem composable, modify the composable contents to be as follows:

ElevatedCard(
    modifier = Modifier
     ...

Summary

In this chapter, we have learned how to add tests for the different layers in our MVVM architecture. We have learned about the importance of adding tests to our apps and how to add unit tests, integration tests, and instrumentation tests.

In the next chapter, we will learn step-by-step how to publish a new app in the Google Play Store. We will walk through how to create a signed app bundle and the things required for us to publish our first app to the Play Store. Additionally, we will learn about some of the Google Play Store policies and how to always stay compliant to avoid our apps from being removed or accounts from being banned.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Kotlin for Android 14
Published in: Apr 2024Publisher: PacktISBN-13: 9781837631711
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Harun Wangereka

Harun Wangereka is a Google Developer Expert for Android and an Android engineer with over seven years of experience and currently working at Apollo Agriculture. Harun is passionate about Android development, never tired of learning, building the tech community, and helping other developers upscale their skills. He is a co-organizer at Droidcon Kenya and a former Kotlin Kenya and Android254 co-organizer. Through these communities, he has been able to impact thousands of developers. He is also an Android author at Kodeco where he has written 8 articles, published a book; Saving Data on Android, Second Edition, and is also a video course instructor. He has given numerous sessions on Android and Kotlin across different communities worldwide.
Read more about Harun Wangereka