Thanks Jose Alcérreca! This solution helped me realize refactoring from theAndroidViewModel to ViewModel will improve local JUnit testing by removing the need to create Application Context.

I recently refactored the main feed of Coinverse’s Open App with the Unidirectional Data Flow (UDF) pattern to organize app logic. A major expectation was that in doing so it would make JUnit testing easier.

I quickly hit this StackOverflow issue with theAndroidViewModel. During JUnit setup and creating the AndroidViewModel, Application Context is required, causing a runtime error.

Directoryapp > src > test > java

ExampleUnitTest.kt

import androidx.test.core.app.ApplicationProviderval context = ApplicationProvider.getApplicationContext<Context>()

java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.

By removing the Application Context dependency and using one of the Dependency Injection libraries to create components in the ViewModel (ie — Repository, Database, Analytics, and etc.), the error above may be avoided.

--

--

--

Open Sourcer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adam Hurwitz

Adam Hurwitz

Open Sourcer

More from Medium

Marsha’s handbag does Eurovision 2022

A social media home for adult baseball players

A Book Announces the Death of Two-State Solution