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 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...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Mastering Kotlin for Android 14
Published in: Apr 2024Publisher: PacktISBN-13: 9781837631711

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