5

嗨,我正在尝试使用 kotlin koin 将变量加载/注入到我的单元测试中,但它没有这样做,并且当我尝试使用变量时出现 no class def found 错误。

下面是我的模块:

    val testModules = module {
        single<NetworkControllerContract> {
            Mockito.mock(NetworkControllerContract::class.java)
        }
        single {
            Room.inMemoryDatabaseBuilder(ApplicationProvider.getApplicationContext<Context>(),
                    PostDatabase::class.java).allowMainThreadQueries().build()
        }
        single<PostRepositoryContract> { PostRepository(get(), get()) }
//            factory { GetAllDataUseCase(get()) }
        single { create<GetAllDataUseCase>() }
        single { GetCommentCountUseCase(get()) }
        single { GetPostDetailsUseCase(get()) }
        single { GetPostListUseCase(get()) }
    }
}

这是我的测试课

class UseCaseUnitTests : KoinTest {

    @get:Rule
    var rule: TestRule = InstantTaskExecutorRule()

    val getAllDataUseCase: GetAllDataUseCase by inject()
    val getCommentCountUseCase: GetCommentCountUseCase by inject()
    val getPostDetailsUseCase: GetPostDetailsUseCase by inject()
    val getPostListUseCase: GetPostListUseCase by inject()
    val networkController: NetworkControllerContract by inject()

    private val testModulesList = listOf(testModules)

    @Before
    fun setup() {
        startKoin(testModulesList)
    }

    @After
    fun cleanUp() {
        stopKoin()
    }

    @Test
    fun testGetAllData() {
        Mockito.`when`(networkController.getUsers()).thenReturn(GlobalScope.async(Dispatchers.Default,
                CoroutineStart.DEFAULT,
                null, { getMockedUser() }))

        Mockito.`when`(networkController.getPosts()).thenReturn(GlobalScope.async(Dispatchers.Default,
                CoroutineStart.DEFAULT,
                null, { getMockedPosts() }))

        Mockito.`when`(networkController.getComments()).thenReturn(GlobalScope.async(Dispatchers.Default,
                CoroutineStart.DEFAULT,
                null, { getMockedComments() }))

        val fetchDataLiveData = getAllDataUseCase.fetchAllData()

        fetchDataLiveData.test()
                .assertHasValue()
                .assertValue(true)
    }

    @Test
    fun testGetCommentCount() {
        getCommentCountUseCase.getCommentCount("1").test()
                .assertHasValue()
                .assertValue(3)
    }

    @Test
    fun testGetPostList() {
        getPostListUseCase.getPostList().test()
                .assertHasValue()
                .assertValue {
                    it.size == 3
                }
    }

    @Test
    fun testGetPostDetails() {
        getPostDetailsUseCase.getPostDetails("2").test()
                .assertHasValue()
                .assertValue {
                    val postDetails = it
                    postDetails.username == "user3"
                }
    }

当 koin 尝试获取注入的值时,我得到的错误是

    java.lang.NoClassDefFoundError: org/koin/test/KoinTest$DefaultImpls
    at com.app.UseCaseUnitTests.getKoin(UseCaseUnitTests.kt:37)
    at com.app.UseCaseUnitTests$$special$$inlined$inject$3.invoke(KoinComponent.kt:53)
    at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
    at com.app.UseCaseUnitTests.getGetPostDetailsUseCase(UseCaseUnitTests.kt)
    at com.app.UseCaseUnitTests.testGetPostDetails(UseCaseUnitTests.kt:120)
    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.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    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.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)
    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 com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

我什至尝试了这里的示例https://insert-koin.io/docs/1.0/quick-references/koin-test/

class MyTest : KoinTest {

    class ComponentA
    class ComponentB(val a: ComponentA)

    val appModule = module {
        single { ComponentA() }
        single { ComponentB(get()) }
    }

    // Lazy inject property
    val componentB : ComponentB by inject()

    @Test
    fun `should inject my components`() {
        startKoin(listOf(appModule))

        // directly request an instance
        val componentA = get<ComponentA>()

        assertNotNull(componentA)
        assertEquals(componentA, componentB.a)
    }
}

没有喜悦,同样的错误

4

1 回答 1

3

I fixed this by using latest koin 1.0.2 version

于 2019-01-16T10:07:50.713 回答