I am trying to get dagger component dependency in my AndroidTest package
Dependencies
implementation "com.google.dagger:dagger:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
debugImplementation 'androidx.test:runner:1.5.1'
debugImplementation 'androidx.test:rules:1.5.0'
debugImplementation "androidx.test:core-ktx:1.5.0"
kaptAndroidTest "com.google.dagger:dagger-compiler:$dagger_version"
Code in AndroidTest package
Component
#Singleton
#Component
interface TestUserComponent{
fun inject(myActivityTest: MyActivityTest)
}
Test file
class MyActivityTest {
lateinit var mTestUserComponent: TestUserComponent
#Before
fun setUp() {
mTestUserComponent = DaggerTestUserComponent.builder().build()
}
}
From above code I am not getting autogenerated file "DaggerTestUserComponent"
Related
I have spent the hole week trying to add hilt dependency injection to my sample note application, android studio have been throwing on me error after an error.It got me mad, any way, in AppModule i have been trying to inject my room database to app repository and then my app repo to my use cases class and at the end injecting use cases class to my sharedViewModel
so this is my AppModule object:
#Module
#InstallIn(SingletonComponent::class)
object AppModule {
#Provides
#Singleton
fun provideNoteDatabase(app: Application): NoteDatabase {
return Room.databaseBuilder(
app,
NoteDatabase::class.java,
NoteDatabase.DATABASE_NAME
).build()
}
#Provides
#Singleton
fun provideNoteRepository(db: NoteDatabase): NotesRepo {
return RepoImplementation(db.noteDao())
}
#Provides
#Singleton
fun provideNoteUseCase(repo: NotesRepo): NoteUseCase {
return NoteUseCase(
getNotesUseCase = GetNotesUseCase(repo),
deleteNoteUseCase = DeleteNoteUseCase(repo),
updateNoteUseCase = UpdateNoteUseCase(repo),
insertNoteUseCase = InsertNoteUseCase(repo)
)
}
}
and this my Application class:
#HiltAndroidApp
class Application : Application()
my edit fragment:
#AndroidEntryPoint
class EditFragment : Fragment() {
private var _binding: FragmentEditBinding? = null
private val binding get() = _binding!!
private val viewModel: SharedViewModel by activityViewModels()
//...
}
my other fragment:
#AndroidEntryPoint
class MainFragment : Fragment() {
private var _binding: FragmentMainBinding? = null
private val binding get() = _binding!!
private val viewModel: SharedViewModel by activityViewModels()
//...
}
by the way also my MainActivity is annotated with #AndroidEntryPoint
my famous viewModel :
#HiltViewModel
class SharedViewModel #Inject constructor(private val noteUseCase: NoteUseCase) :
ViewModel() {...}
this is project level build.gradle:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
def nav_version = "2.5.2"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.44'
}
}
plugins {
id 'com.android.application' version '7.3.0' apply false
id 'com.android.library' version '7.3.0' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}
and module level build.gradle:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android'
id 'kotlin-kapt'
id "androidx.navigation.safeargs"
id 'com.google.dagger.hilt.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.stayin"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
dataBinding true
viewBinding true
}
namespace 'com.example.stayin'
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
def lifecycle_version = "2.4.1"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
// coroutines for getting off the UI thread
def coroutines = "1.6.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"
//shared preferences dependency
implementation 'androidx.preference:preference-ktx:1.2.0'
// Room dependency
def room_version = "2.4.3"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
// Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
//navigation component dependency
implementation "androidx.navigation:navigation-fragment-ktx:2.5.2"
implementation "androidx.navigation:navigation-ui-ktx:2.5.2"
//Dagger - Hilt
implementation 'com.google.dagger:hilt-android:2.44'
kapt 'com.google.dagger:hilt-compiler:2.44'
// For instrumentation tests
androidTestImplementation 'com.google.dagger:hilt-android-testing:2.44'
kaptAndroidTest 'com.google.dagger:hilt-compiler:2.44'
// For local unit tests
testImplementation 'com.google.dagger:hilt-android-testing:2.44'
kaptTest 'com.google.dagger:hilt-compiler:2.44'
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
}
if can any one help me to find what is wrong and explained why, i will be so thankful towards him. i rally need to pass this so i can level up in my career.
Remove below deprecated dependency:
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
(It was deprecated since dagger-2.34 version)
proof:
https://github.com/google/dagger/releases/tag/dagger-2.34
Also Try to upgrade your lifecycle version as below:
def lifecycle_version = "2.5.1"
add below lines after dependency section in build.gradle(app):
kapt { correctErrorTypes true }
follow official documentation:
https://developer.android.com/training/dependency-injection/hilt-android
https://dagger.dev/hilt/view-model.html
I'm testing hilt with a simple project, what I want to achieve is to generate an instance of my MainViewModel with Hilt this is what I have done so far
MainActivity
#AndroidEntryPoint
class MainActivity : AppCompatActivity() {
...
}
MainFragment
#AndroidEntryPoint
class MainFragment : Fragment(),MainAdapter.OnTragoClickListener {
private val viewModel by activityViewModels<MainViewModel>()
...
}
MainViewModel
class MainViewModel #ViewModelInject constructor(private val repo:Repo):ViewModel(){
...
}
RepoImpl
class RepoImpl #Inject constructor(private val dataSource: DataSource): Repo {
...
}
DataSourceImpl
class DataSourceImpl #Inject constructor(private val tragosDao: TragosDao): DataSource{
...
}
Now , this is the architecture the app follows, here Repo and DataSource are simple interfaces that I use.
So after this I generate all that hilt requires to generate the instances
BaseApplication
#HiltAndroidApp
class BaseApplication: Application() {
}
AppModule
#Module
#InstallIn(ApplicationComponent::class)
object AppModule {
#Singleton
#Provides
fun provideRoomInstance(
#ApplicationContext context: Context
) = Room.databaseBuilder(
context,
AppDatabase::class.java,
"tabla_tragos")
.build()
#Singleton
#Provides
fun provideTragosDao(db: AppDatabase) = db.tragoDao()
}
The module above provides tragoDao() so I can access it in my DataSourceImpl, since I need an unique instance of this database I use #Singleton on its provide
Then I just create another module that will let hilt know about the implementations of the interfaces above
#Module
#InstallIn(ActivityRetainedComponent::class)
abstract class ActivityModule {
#Binds
abstract fun bindDataSource(dataSource:DataSourceImpl): DataSource
#Binds
abstract fun bindRepo(repo: RepoImpl): Repo
}
Since I need an instance of the MainViewModel , I scope this module with ActivityRetainedComponent
After compiling the app I get this error
java.lang.RuntimeException: Cannot create an instance of class
com.g.tragosapp.ui.viewmodel.MainViewModel
Dependencies
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
//Navigation Components
implementation "androidx.navigation:navigation-fragment-ktx:2.3.0"
implementation "androidx.navigation:navigation-ui-ktx:2.3.0"
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
//ViewModel y LiveData
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
// KTX - Viewmodel Y Livedata
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.3.0-alpha05"
implementation "androidx.fragment:fragment-ktx:1.2.5"
implementation "androidx.activity:activity-ktx:1.1.0"
//utils
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
//Corutinas
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.3"
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.0'
implementation 'com.github.chrisbanes:PhotoView:2.3.0'
//Room
implementation 'androidx.room:room-ktx:2.2.5'
implementation "androidx.room:room-runtime:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"
//Hilt
implementation "com.google.dagger:hilt-android:2.28-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
I have also added
implementation "androidx.fragment:fragment-ktx:1.2.5"
implementation "androidx.activity:activity-ktx:1.1.0"
implementation "androidx.core:core:1.3.1"
which has not made any difference
class RepoImpl
Should be
#Singleton class RepoImpl
And same for DataSourceImpl
Then change #InstallIn(ActivityRetainedComponent::class) to #InstallIn(SingletonComponent::class) (used to be ApplicationComponent)
And also make sure to have all these deps (at the time of writing):
buildscript {
ext {
dagger_version = '2.41'
}
dependencies {
classpath "com.google.dagger:hilt-android-gradle-plugin:$dagger_version"
}
and
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'kotlin-kapt'
implementation "com.google.dagger:dagger:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
implementation "com.google.dagger:hilt-android:$dagger_version"
kapt "com.google.dagger:hilt-android-compiler:$dagger_version"
kaptTest "com.google.dagger:hilt-android-compiler:$dagger_version"
kaptAndroidTest "com.google.dagger:hilt-android-compiler:$dagger_version"
kapt 'androidx.hilt:hilt-compiler:1.0.0'
I'm writing some JUnits for my Android app which use Dagger2. To demonstrate my problem I simplified my test.
In gradle I have those dependencies related to Junits and dagger:
//dependency injection
api "com.google.dagger:dagger:$dagger_version"
annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"
api "com.google.dagger:dagger-android:$dagger_version"
api "com.google.dagger:dagger-android-support:$dagger_version"
annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version"
//unit testing
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
//dagger testing
testImplementation "com.google.dagger:dagger:$dagger_version"
testAnnotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"
Here is my Test class
#RunWith(MockitoJUnitRunner.class)
public class Test extends TestCase {
#Inject
String greating;
#Before
public void setUp() {
TestComponent component = DaggerTestComponent.builder().testModule(new TestModule()).build();
component.inject(this);
}
#org.junit.Test
public void test() {
assertEquals("Hello!", greating);
}
}
Component for DI
#Singleton
#Component(modules = {TestModule.class})
public interface TestComponent {
void inject(Test test);
}
Test module for DI
#Module
public class TestModule {
#Singleton
#Provides
public String providesHello() {
return "Hello!";
}
}
My problem is that during the execution of the test, DaggerTestComponent is generated, but Android Studio underlines with red color the new TestModule() on line where is TestComponent component = DaggerTestComponent.builder().testModule(new TestModule()).build(); On hower it shows me testModule(TestModule) in builder cannot be aplied to mytestpackage.TestModule. If I open generated DaggerTestComponent it look ok but contains error cannot resolve symbol TestModule on it's every occurence.
DaggerTestComponent is generated under
\app\build\generated\ap_generated_sources\debugUnitTest\out\mytestpackage
My test and related classes are located in
\app\src\test\java\mytestpackage
Regardless this, the Test works fine and ends as passed.
How do I get rid off this annoying error? Do I need to configure additional classpaths in gradle or set something else in AndroidStudio? Thanks in advance!
Finally problem solved by adding
android.sourceSets {
test.java.srcDirs +=
[file("$buildDir/generated/ap_generated_sources/debugUnitTest/out")]
}
Thanks to Daivid for solution - https://stackoverflow.com/a/61329262/4345570
I want to inject dependencies in android tests using dagger.
I've created a simple App with an AppComponent extending AndroidInjector.
That works for the App itself (the code is built in /build by dagger).
Then, for /test I've also created the AppComponentTest and AppTest.
It took me some time to realised that I need to create a naive test and run it to let gradle create the /build for test.
Now the code is there, but there is some error with that generated code, as you can see here:
AppComponent.kt
#Singleton
#Component(
modules = [
AndroidSupportInjectionModule::class
]
)
interface AppComponent : AndroidInjector<App> {
#Component.Factory
interface Factory {
fun create(#BindsInstance application: Application): AppComponent
}
}
App.kt
open class App : Application(), HasAndroidInjector {
#Inject
lateinit var dispatchingAndroidInjector: DispatchingAndroidInjector<Any>
override fun androidInjector(): AndroidInjector<Any> = dispatchingAndroidInjector
override fun onCreate() {
super.onCreate()
setupDagger()
}
#VisibleForTesting
open fun setupDagger() {
DaggerAppComponent
.factory()
.create(this)
.inject(this)
}
}
Then under the same structure but in /test I've created this:
AppComponentTest.kt
#Singleton
#Component(
modules = [
AndroidSupportInjectionModule::class
]
)
interface AppComponentTest : AppComponent {
#Component.Factory
interface Factory {
fun create(
#BindsInstance application: Application
): AppComponentTest
}
}
AppTest.kt
class AppTest : App() {
override fun setupDagger() {
DaggerTestAppComponent
.factory()
.create(this)
.inject(this)
}
}
And this is the build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.dagger"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
def dagger_version = "2.27"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
testImplementation 'junit:junit:4.13'
kaptTest "com.google.dagger:dagger-compiler:$dagger_version"
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Also the naive test:
ExampleUnitTest.kt
#RunWith(JUnit4::class)
class ExampleUnitTest {
#Test
fun test() {
assert(true)
}
}
I've also tried with other approach using this:
AppComponentTest2.kt
#Singleton
#Component(modules = [])
interface AppComponentTest2 {
fun inject(test: ExampleUnitTest2)
}
ExampleUnitTest2.kt
#RunWith(JUnit4::class)
class ExampleUnitTest2 {
#Test
fun test() {
DaggerAppComponentTest2
.builder()
.build()
.inject(this)
assert(true)
}
}
And also the same error:
Btw, this is a repo with that code:
https://gitlab.com/fitu/dagger
There are two branches there first_approach and second_approach
EDIT 1:
I had other repo where I was able to do this injection and now I've tried and it doesn't work.
Comparing dependencies I have:
New project:
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.61'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61'
implementation 'com.google.dagger:dagger-android:2.27'
implementation 'com.google.dagger:dagger-android-support:2.27'
kapt 'com.google.dagger:dagger-android-processor:2.27'
kapt 'com.google.dagger:dagger-compiler:2.27'
kaptTest 'com.google.dagger:dagger-compiler:2.27'
Old project:
classpath 'com.android.tools.build:gradle:3.2.0'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.70'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.61'
implementation'com.google.dagger:dagger-android-support:2.14.1'
kapt 'com.google.dagger:dagger-compiler:2.14.1'
kaptTest 'com.google.dagger:dagger-compiler:2.14.1'
So it seems is not a dagger or kotlin version.
Maybe it's related with Android Studio?
I'm not sure about the version of AS when I'd created that old project, but my current AS version is: 3.6.1
EDIT 2:
I've deleted almost everything in the project, the main folder with App and everything:
The only reminding was two classes: AppComponentTest.kt and ExampleUnitTest.kt
#Singleton
#Component(modules = [])
interface AppComponentTest
class ExampleUnitTest {
#Test
fun test() {
DaggerAppComponentTest
.builder()
.build()
assert(true)
}
}
The error persist, so I'm guessing the problem could be a combinations of AS version, Kotlin version and/or Dagger version.
I want to write a unit test. Therefore I need MutableLiveData. I started with a very basic test for setup but I cannot instantiate a MutableLiveData object. I is always null when I run the test. Do I have to mock anything? Any suggestions?
#RunWith(MockitoJUnitRunner.class)
public class DefaultLiveDataTest {
private static final int EXPECTED = 5;
private final MutableLiveData<Integer> underTest = new MutableLiveData<>();
#Test
public void exampleTest() {
underTest.setValue(EXPECTED); //underTest is Null
assertEquals(underTest.getValue().intValue(), EXPECTED);
}
}
java.lang.NullPointerException
at android.arch.core.executor.DefaultTaskExecutor.isMainThread(DefaultTaskExecutor.java:58)
at android.arch.core.executor.ArchTaskExecutor.isMainThread(ArchTaskExecutor.java:116)
at android.arch.lifecycle.LiveData.assertMainThread(LiveData.java:434)
at android.arch.lifecycle.LiveData.setValue(LiveData.java:279)
at android.arch.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
at com.mypackage.DefaultLiveDataTest.test_that_live_data_has_default_value(DefaultLiveDataTest.java:22)
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId 'com.mypackage.title'
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName '1.0'
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
testOptions {
unitTests.returnDefaultValues = true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-vector-drawable:27.1.1'
implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'android.arch.lifecycle:viewmodel:1.1.1'
implementation 'android.arch.lifecycle:livedata:1.1.1'
annotationProcessor 'android.arch.lifecycle:compiler:1.1.1'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
annotationProcessor 'org.androidannotations:androidannotations:4.4.0'
implementation 'org.androidannotations:androidannotations-api:4.4.0'
compileOnly 'org.projectlombok:lombok:1.16.20'
annotationProcessor 'org.projectlombok:lombok:1.16.20'
}
Looks like you are missing the android.arch.core:core-testing dependency.
testImplementation "android.arch.core:core-testing:1.1.1"
This allows you to use the InstantTaskExecutorRule in your test, which will get rid of the isMainThread call.
https://developer.android.com/reference/android/arch/core/executor/testing/InstantTaskExecutorRule.html
#Rule
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();
Add an executor InstantTaskExecutorRule() as a member of the Test class
A JUnit Test Rule that swaps the background executor used by the
Architecture Components with a different one which executes each task
synchronously. You can use this rule for your host side tests that use
Architecture Components.
//#RunWith(JUnit4::class) // For JUnit4
#ExtendWith(InstantExecutorExtension::class) // For JUnit5
class FilterViewModelTest {
#Rule #JvmField
val instantTaskExecutorRule = InstantTaskExecutorRule()
#Test
fun test() {
//Here you don't ask if isMainThread
}
}
build.gradle(:mobile)
android {
//...
dependencies {
//...
testImplementation 'androidx.arch.core:core-testing:2.1.0'
androidTestImplementation 'androidx.arch.core:core-testing:2.1.0'
}
}
GL
InstantTaskExecutorRule
I had this error and solved it by adding InstantTaskExecutorRule:
private lateinit var contactProfileViewModel: ContactProfileViewModel
private val getStatusesForContact: GetStatusesForContact = mockk(relaxed = true)
private val getStory: GetUserLastStory = mockk(relaxed = true)
private val successStatusesCaptor = slot<((List<StatusDomain>) -> Unit)>()
private val successStoryCaptor = slot<((List<StoryDomain>) -> Unit)>()
#get:Rule
val rule: TestRule = InstantTaskExecutorRule()
#Before
fun setUp(){
contactProfileViewModel = ContactProfileViewModel(getStatusesForContact, getStory)
}