在我的测试模块中提供 HiltTestApplication 作为依赖项失败

时间:2021-05-26 12:45:44

标签: android testing dagger-2 dagger-hilt instrumented-test

我已遵循 this 指南并尝试使用 HILT 运行插桩测试。但是在我的测试模块中提供 HiltTestApplication 作为依赖项时失败了。 下面是我的模块类-

@ExperimentalCoroutinesApi
@FlowPreview
@Module
@TestInstallIn(components = [SingletonComponent::class],
    replaces = [ProductionModule::class])
object TestModule {

    @JvmStatic
    @Singleton
    @Provides
    fun provideRecipeDb(app: HiltTestApplication): RecipeDatabase {
        return Room
            .inMemoryDatabaseBuilder(app, RecipeDatabase::class.java)
            .fallbackToDestructiveMigration()
            .build()
    }
}

出现以下错误-

<块引用>

C:\Users\bhuvn\AndroidStudioProjects\Dairy\app\build\generated\source\kapt\debugAndroidTest\com\bhuvnesh\diary\framework\dataSource\cache\RecipeDaoServiceTests_HiltComponents.java:128: 错误:[匕首/缺少绑定] 无法提供 dagger.hilt.android.testing.HiltTestApplication 没有@Inject 构造函数或@Provides 注释的方法。
公共抽象静态类 SingletonC 实现 RecipeDaoServiceTests_GeneratedInjector, ^ dagger.hilt.android.testing.HiltTestApplication 被注入 com.bhuvnesh.diary.di.TestModule.provideRecipeDataFactory(应用程序, �) com.bhuvnesh.diary.framework.dataSource.data.RecipesDataFactory 注入 com.bhuvnesh.diary.framework.dataSource.cache.RecipeDaoServiceTests.recipeDataFactory com.bhuvnesh.diary.framework.dataSource.cache.RecipeDaoServiceTests 是 注射在 com.bhuvnesh.diary.framework.dataSource.cache.RecipeDaoServiceTests_GeneratedInjector.injectTest(com.bhuvnesh.diary.framework.dataSource.cache.RecipeDaoServiceTests) 也可在以下网址索取: com.bhuvnesh.diary.di.TestModule.provideRecipeDb(app)

我也尝试通过使用-

在同一模块中手动提供依赖项
    @JvmStatic
    @Singleton
    @Provides
    fun provideHiltTestApplication():HiltTestApplication{
        return HiltTestApplication()
    }

但它提供了一个空对象,导致空指针异常。

0 个答案:

没有答案
相关问题