Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Mocking dependencies in UI test using Koin

 

 
Jobin Lawrance
Senior Software Engineer @ Dealersocket, Recro
Published: December 25, 2020
Tweet
Share
 

Image by Jibin Lawrance

 

This is a simple way to mock runtime dependencies in Instrumentation tests when using Koin Library

First, let’s take a look at how an application would mostly be set up when using Koin for DI.

 

This code will not compile, this is only for a simple run through.

 

Now as an example, let’s take a look at some UI code that consumes these dependencies and for which we will write some Espresso tests.

 

 

In the above activity, HomeViewModel is lazy injected using an extension function provided by Koin. 
We are observing a LiveData returned by this HomeViewModel and displaying the UI based on the value of Result.

For the uninitiated, Result is just a wrapper class used to hold different types of values like data, exceptions, etc. in the same object itself. You can find detailed info here.

Assuming that our HomeViewModel and HomeInteractor code is as follows:

 

 

Now to mock these dependencies in our instrumentation test, we need to add the below dependencies:

 

androidTestImplementation "org.koin:koin-test:2.x.x"androidTestImplementation "org.mockito:mockito-android:2.22.0"

 

Note: Since classes in Kotlin are final by default, it won’t be possible to mock them in instrumentation tests by Mockito. A simple and naive way to get around this will be to open the class but a better alternative can be found here or here

 

Mocking/Spying in Test Class

To mock the dependencies injected by Koin, we extend the KoinTestclass and override the module with the mocked dependency like below:

 

 

There may be some scenarios where instead of mocking the whole class, we would like to just mock a part of the real implementation.

In such a case, we first fetch the original dependency from Koin and create a spy of it, then override it with this spy.

 

 

Mocking in Test Application

On the off chance that we want to override a dependency throughout the test application, then instead of overriding the dependencies in every test class, we can just do it once by extending the Application class in the androidTestdirectory like

 

 

and to wrap this up we have to create a custom runner and add it to our build.gradle file

 

 

References

  1. https://proandroiddev.com/testing-with-koin-ade8a46eb4d
  2. https://start.insert-koin.io/#/getting-started/testing
  3. https://start.insert-koin.io/#/getting-started/koin-for-android

 

 

 

Tags: Dependency Injection, Koin, Espresso, Mockito, Android App Development

 

View original article at: 


 

Originally published: December 15, 2020

Android News
Evolution of Android Update SystemEvolution of Android Update System
Evolution of Android Update SystemEvolution of Android Update System

By Ivan Kuten

So, how can you update Android on mobile devices? While developing software for Smart TVs and Android-based set-top boxes, we’ve narrowed it down to four ways, discarding some very exotic options:

By ProAndroidDev -
Android News
Happy Railway
Happy Railway

By Hadi Lashkari Ghouchani

This post is on the tail of Railway Oriented Programming in Kotlin by Antony Harfield. So you need to read it first and continue here. As it’s obvious I really liked it and tried it out. It needs every process have a result like

By ProAndroidDev -
Android News
Unit Tests and Concurrency
Unit Tests and Concurrency

By Stojan Anastasov

Once Retrofit added RxJava support, RxJava became my go-to concurrency framework for writing Android apps. One of the great things about RxJava is the excellent testing support. It includes TestObserver, TestScheduler, RxJavaPlugins so you can switch your schedulers in tests.

By ProAndroidDev -
Android News
When Compat libraries will not save you
When Compat libraries will not save you

By Danny Preussler

And why you should avoid using the “NewApi” suppression! The idea of “Compat” libraries was probably one of the key aspects of Android dominating the mobile space. Other than with iOS, Android users often could not update their operating system after a new version launch, simply as their phones won’t allow them to, the Android problem of fragmentation.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One