# Unit Testing RxJava Or Coroutine Code With Constructor Injection

DevFeed: [Unit Testing RxJava Or Coroutine Code With Constructor Injection](<https://devfeed.tech/articles/unit-testing-rxjava-or-coroutine-code-with-constructor-injection-22846.md>)

Original publisher: [Read original article](<http://androidessence.com/unit-testing-async-code/>)

Author: Adam McNeilly

Published: 2019-04-30T00:00:00Z

Content type: tutorial

Language: en

Sources: [Android Essence](<https://devfeed.tech/sources/android-essence.md>)

Topics: [Unit testing](<https://devfeed.tech/topics/unit-testing.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Coroutines](<https://devfeed.tech/topics/coroutines.md>), [API](<https://devfeed.tech/topics/api.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [apis](<https://devfeed.tech/tags/apis.md>), [constructor](<https://devfeed.tech/tags/constructor.md>), [coroutine](<https://devfeed.tech/tags/coroutine.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [rxjava](<https://devfeed.tech/tags/rxjava.md>), [testing](<https://devfeed.tech/tags/testing.md>), [unit-testing](<https://devfeed.tech/tags/unit-testing.md>)

## AI overview

This tutorial explains how to unit test asynchronous Android code written with RxJava or Kotlin coroutines. It recommends injecting schedulers or dispatchers through constructors so tests can replace them with immediate, test-friendly implementations and avoid unavailable Android main-thread or looper behavior.

## Source excerpt

Putting aside the long lasting debate right now about whether you should use RxJava or coroutines for your asynchronous code on Android, both camps often hit the same problem. How do I write unit tests for this? Unit testing asynchronous code is tricky, because we may need to know how to properly test callback APIs, or perhaps we just want things to run instantly and not worry about thread changes. We may also be wondering how to handle not having a "main" thread in a junit test, unlike a connected test. This post will be focusing on handling that last one.