[英]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
可能没有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.