将 ViewModelProvider.Factory 的提供者注入 esspresso 测试
Inject provider of ViewModelProvider.Factory into esspresso test
我有 ViewModel.Factory
的自定义实现,由 Dagger2
注入的 lambda 提供
interface ViewModelFactoryComponent {
val factoryProvider: (Bundle?) -> ViewModelProvider.Factory
}
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
在应用程序中一切正常,但当我尝试配置 Espresso
测试时出现问题。这是 dagger 测试组件配置:
@Singleton
@Component(modules = [ApplicationModule::class, ViewModelModule::class])
interface TestComponent : ApplicationComponent
现在有什么问题 - 像这样测试由 dagger 生成函数生成的组件实现
@Override
public Function1<Bundle, ViewModelProvider$Factory> getFactoryProvider() {
return bindViewModelFactoryProvider.get();
}
这会产生编译错误,而不是像在真实应用中那样:
@Override
public Function1<Bundle, ViewModelProvider.Factory> getFactoryProvider() {
return bindViewModelFactoryProvider.get();
}
首先我认为这是 ViewModelProvider.Factory
可见性的情况,但所有 build.gradle
修改都没有帮助。我遇到了完全缺乏想法的情况,所以我会很高兴至少有一些建议。
更新
我创建了一个空项目来重现这个错误,它应该是完全可重复的。
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()
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)
}
}
这是所有依赖项:
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'
应用程序构建没有任何问题,但是当我尝试启动 appLaunchesSuccessfully()
测试时,由于上述原因出现编译错误。
编辑
所以,我发现没有 kaptAndroidTest 'com.google.dagger:dagger-compiler:2.21'
测试项目可以成功构建。
坏消息是没有它匕首组件 class 将不会生成。
认为您需要这个插件:
apply plugin: 'kotlin-kapt'
这些 dependencies
:
kapt "com.google.dagger:dagger-compiler:2.21"
implementation "com.google.dagger:dagger:2.21"
并启用选项 generateStubs
:
kapt {
generateStubs = true
}
有很多similar questions ...also see the user's guide。
kaptAndroidTest
可能没用。
根据https://github.com/google/dagger/issues/1454 and https://youtrack.jetbrains.net/issue/KT-27936第一个link
下只有一个临时解决方案
我有 ViewModel.Factory
的自定义实现,由 Dagger2
interface ViewModelFactoryComponent {
val factoryProvider: (Bundle?) -> ViewModelProvider.Factory
}
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
在应用程序中一切正常,但当我尝试配置 Espresso
测试时出现问题。这是 dagger 测试组件配置:
@Singleton
@Component(modules = [ApplicationModule::class, ViewModelModule::class])
interface TestComponent : ApplicationComponent
现在有什么问题 - 像这样测试由 dagger 生成函数生成的组件实现
@Override
public Function1<Bundle, ViewModelProvider$Factory> getFactoryProvider() {
return bindViewModelFactoryProvider.get();
}
这会产生编译错误,而不是像在真实应用中那样:
@Override
public Function1<Bundle, ViewModelProvider.Factory> getFactoryProvider() {
return bindViewModelFactoryProvider.get();
}
首先我认为这是 ViewModelProvider.Factory
可见性的情况,但所有 build.gradle
修改都没有帮助。我遇到了完全缺乏想法的情况,所以我会很高兴至少有一些建议。
更新 我创建了一个空项目来重现这个错误,它应该是完全可重复的。
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()
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)
}
}
这是所有依赖项:
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'
应用程序构建没有任何问题,但是当我尝试启动 appLaunchesSuccessfully()
测试时,由于上述原因出现编译错误。
编辑
所以,我发现没有 kaptAndroidTest 'com.google.dagger:dagger-compiler:2.21'
测试项目可以成功构建。
坏消息是没有它匕首组件 class 将不会生成。
认为您需要这个插件:
apply plugin: 'kotlin-kapt'
这些 dependencies
:
kapt "com.google.dagger:dagger-compiler:2.21"
implementation "com.google.dagger:dagger:2.21"
并启用选项 generateStubs
:
kapt {
generateStubs = true
}
有很多similar questions ...also see the user's guide。
kaptAndroidTest
可能没用。
根据https://github.com/google/dagger/issues/1454 and https://youtrack.jetbrains.net/issue/KT-27936第一个link
下只有一个临时解决方案