【发布时间】:2019-09-15 15:12:05
【问题描述】:
我正在开始我的 viewModel 的单元测试,我需要注入一个存储库依赖项,但是当我运行第一个验证 Koin 是否正在运行的测试时,会引发异常:
org.koin.core.error.InstanceCreationException:无法为 [type:Single,primary_type:'com.ramattec.meussaloes.data.repository.service.ServiceRepository'] 创建实例
at org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:61)
at org.koin.core.instance.SingleDefinitionInstance.get(SingleDefinitionInstance.kt:40)
at org.koin.core.definition.BeanDefinition.resolveInstance(BeanDefinition.kt:70)
at org.koin.core.scope.Scope.resolveInstance(Scope.kt:165)
at org.koin.core.scope.Scope.get(Scope.kt:128)
at com.ramattec.meussaloes.ui.services.ServicesViewModelTest$$special$$inlined$inject$1.invoke(KoinTest.kt:51)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.ramattec.meussaloes.ui.services.ServicesViewModelTest.getRepository(ServicesViewModelTest.kt)
at com.ramattec.meussaloes.ui.services.ServicesViewModelTest.Verificar se Koin está funcionando corretamente(ServicesViewModelTest.kt:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.mockito.internal.runners.DefaultInternalRunner$1$1.evaluate(DefaultInternalRunner.java:44)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.DefaultInternalRunner$1.run(DefaultInternalRunner.java:74)
at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:80)
at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39)
at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:163)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
原因:java.lang.IllegalStateException:默认 FirebaseApp 未在此进程中初始化为 null。确保首先调用 FirebaseApp.initializeApp(Context)。 在 com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@17.1.0:186) 在 com.google.firebase.database.FirebaseDatabase.getInstance(com.google.firebase:firebase-database@@17.0.0:56) 在 com.ramattec.meussaloes.data.repository.service.ServicesRepositoryImpl.(ServicesRepositoryImpl.kt:19) 在 com.ramattec.meussaloes.ui.services.ServicesViewModelTest$before$1$1$1.invoke(ServicesViewModelTest.kt:38) 在 com.ramattec.meussaloes.ui.services.ServicesViewModelTest$before$1$1$1.invoke(ServicesViewModelTest.kt:27) 在 org.koin.core.instance.DefinitionInstance.create(DefinitionInstance.kt:54)
@RunWith(MockitoJUnitRunner::class)
class ServicesViewModelTest: KoinTest{
private val repository: ServiceRepository by inject()
private val servicesViewModel: ServicesViewModel by inject()
@Before
fun before(){
startKoin {
modules(
module {
single<ServiceRepository> { ServicesRepositoryImpl()}
single { ServicesViewModel(get()) }
})
}
}
@Test
fun `Verify if Koin is running`(){
assertEquals(repository, servicesViewModel.repository)
}
}
我愿意接受建议
【问题讨论】:
-
问题是 FirebaseApp 没有初始化,firebase 插件在应用模块上使用时使用 google-service.json,但是为了测试你必须自己做看看这个例子 repo @ 987654321@amd 顺便说一句,您应该集成测试 firebase 而不是模拟,您可以将预定义的结果传递给 firebase 请求和单元测试的结果,但 firebase 应该是集成
标签: android unit-testing mockito