Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin Lazy loading not working in Junit test

Hi i am trying to load/inject variables into my unit test using kotlin koin and it fails to do so and i get a no class def found error when i try to use the variables.

Below is my module:

    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()) }
    }
}

here is my test class

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"
                }
    }

the error i get is this when koin tries to get the injected value

    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)

I even tried the example from here 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)
    }
}

No Joy, same error

like image 426
Jonathan Avatar asked Oct 16 '22 08:10

Jonathan


1 Answers

I fixed this by using latest koin 1.0.2 version

like image 117
Jonathan Avatar answered Oct 20 '22 15:10

Jonathan