Android单元测试模拟Firebase

时间:2019-09-15 15:12:05

标签: android unit-testing mockito

我正在启动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 $之前$ 1 $ 1 $ 1.invoke(ServicesViewModelTest.kt:38)     在com.ramattec.meussaloes.ui.services.ServicesViewModelTest $之前$ 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)
    }
}

我愿意接受建议

2 个答案:

答案 0 :(得分:0)

您不模拟甚至不对Firebase进行单元测试,也不对模型和DAO进行单元测试。

答案 1 :(得分:0)

就我而言,我不需要注入存储库,正确的是:

@Mock
private val repository: ServiceRepository
private val servicesViewModel: ServicesViewModel by inject()

@Before
fun before(){
    startKoin {
        modules(
            module {
                single { ServicesViewModel(repository) }
            })
    }
}

现在我可以为ViewModel编写测试了!感谢您的回答