简体   繁体   English

将ViewModelProvider.Factory的提供程序注入espresso测试

[英]Inject provider of ViewModelProvider.Factory into esspresso test

I have custom implementation of ViewModel.Factory which is provided by lambda injected by Dagger2 我有ViewModel.Factory自定义实现,它由Dagger2注入的lambda提供

interface ViewModelFactoryComponent {
    val factoryProvider: (Bundle?) -> ViewModelProvider.Factory
}

Dagger implementation looks like below: Dagger实现如下所示:

@Module
class ViewModelModule {
    @Provides
    @Singleton
    fun bindViewModelFactory(creators: Map<Class<out ViewModel>, @JvmSuppressWildcards Provider<(Bundle?) -> ViewModel>>): (Bundle?) -> ViewModelProvider.Factory {
        return { ViewModelFactory(creators, it) }
    }
}

@Singleton
@Component(modules = [ ApplicationModule::class, ViewModelModule::class])
interface ApplicationComponent : ViewModelFactoryComponent

In application everything works like a charm but problem appeared when I've tried to configure Espresso test. 在应用程序中,所有内容都像魅力一样起作用,但是当我尝试配置Espresso测试时出现了问题。 Here's dagger test component configuration: 这是匕首测试组件的配置:

@Singleton
@Component(modules = [ApplicationModule::class, ViewModelModule::class])
interface TestComponent : ApplicationComponent

Now what is the problem - test component implementation generated by dagger generate function like this 现在出了什么问题-由dagger生成的测试组件实现生成了这样的函数

 @Override
  public Function1<Bundle, ViewModelProvider$Factory> getFactoryProvider() {
    return bindViewModelFactoryProvider.get();
  }

which generate compilation error, instead like in real app: 生成编译错误,而不是像在真实应用中那样:

  @Override
  public Function1<Bundle, ViewModelProvider.Factory> getFactoryProvider() {
    return bindViewModelFactoryProvider.get();
  }

First I thought it's a case of ViewModelProvider.Factory visibility, but all build.gradle modification didn't help. 首先,我认为这是ViewModelProvider.Factory可见性的一种情况,但是所有build.gradle修改都build.gradle I've met with total lack of idea so I will be greatfull for at least some suggestions. 我完全不了解我的想法,所以我至少会提出一些建议。

UPDATE I have created empty project to reproduce this error and it suppose to be completly repeatable. 更新我创建了一个空项目来重现此错误,并且它应该是完全可重复的。

File in main directory: main目录中的文件:


@Singleton
@Component(modules = [ViewModelModule::class])
interface ApplicationComponent : ViewModelFactoryComponent

@Module
class ViewModelModule {

    @Provides
    @Singleton
    fun bindViewModelFactory(): () -> ViewModelProvider.Factory {
        return { ViewModelFactory() }
    }
}

interface ViewModelFactoryComponent {
    val factoryProvider: () -> ViewModelProvider.Factory
}

class ViewModelFactory @Inject constructor() : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return modelClass.newInstance()
    }
}

class MainActivity : AppCompatActivity()

File in androidTest directory: androidTest目录中的文件:

@Singleton
@Component(modules = [ViewModelModule::class])
interface TestComponent : ApplicationComponent

@RunWith(AndroidJUnit4::class)
class TestCase {
    @get:Rule
    val activityTestRule = ActivityTestRule(MainActivity::class.java, false, false)

    @Test
    fun appLaunchesSuccessfully() {
        ActivityScenario.launch(MainActivity::class.java)
    }
}

And this are all dependencies: 这些都是依赖项:

    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.dagger:dagger:2.21'
    kapt 'com.google.dagger:dagger-compiler:2.21'
    kaptAndroidTest 'com.google.dagger:dagger-compiler:2.21'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.1'

Application is build without any problem but when I try to launch appLaunchesSuccessfully() test, compile error from reason above, appear. 应用程序的构建没有任何问题,但是当我尝试启动appLaunchesSuccessfully()测试时,出现上述原因的编译错误。

Edit So, I figured out that without kaptAndroidTest 'com.google.dagger:dagger-compiler:2.21' test project can be build successfully. 编辑因此,我发现没有kaptAndroidTest 'com.google.dagger:dagger-compiler:2.21'测试项目可以成功构建。
The bad new is that without it dagger component class won't generate. 糟糕的是,没有它,匕首组件类将不会生成。

think you need this plugin: 认为您需要此插件:

apply plugin: 'kotlin-kapt'

with these dependencies : 具有这些dependencies

kapt "com.google.dagger:dagger-compiler:2.21"
implementation "com.google.dagger:dagger:2.21"

and enable option generateStubs : 并启用选项generateStubs

kapt {
    generateStubs = true
}

there are lots of similar questions ...also see the user's guide . 有很多类似的问题 ...另请参阅用户指南

kaptAndroidTest might be useless. kaptAndroidTest可能没有用。

根据https://github.com/google/dagger/issues/1454https://youtrack.jetbrains.net/issue/KT-27936 ,只有第一个链接下的临时解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 ViewModelProvider.Factory 注入片段 - How to inject ViewModelProvider.Factory into a fragment 使用匕首将 ViewModelProvider.Factory 注入不同的 Activity - Inject ViewModelProvider.Factory to different Activity using dagger 匕首2 viewmodels和ViewModelProvider.Factory - dagger 2 viewmodels and ViewModelProvider.Factory 获取ViewModel ViewModelProvider.Factory和应用程序上下文 - Get ViewModel ViewModelProvider.Factory and Application Context Dagger 2 ViewModelProvider.Factory绑定多次 - Dagger 2 ViewModelProvider.Factory bound multiple times ViewModelProvider.Factory 总是返回一个视图模型 - ViewModelProvider.Factory always return one viewmodel ViewModelProvider.Factory 和 ViewModelProvider.NewInstanceFactory 有什么区别? - What are the differences between ViewModelProvider.Factory and ViewModelProvider.NewInstanceFactory? 使用Android上的Dagger和Java,ViewModelProvider.Factory在片段上保持为空 - ViewModelProvider.Factory remains null on fragment using Dagger and Java on Android 从 AndroidViewModel 扩展时如何使用 ViewModelProvider.Factory - How to use a ViewModelProvider.Factory when extends from AndroidViewModel 为什么为我的viewModel类实现ViewModelProvider.Factory很重要? - Why it is important to implement a ViewModelProvider.Factory for my viewModel class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM