Search code examples
androidunit-testingtestingkotlinkoin

How to fix NoSuchMethodError on startKoin() method


I'm trying to launch tests with Koin DI help like in the example https://insert-koin.io/docs/2.0/documentation/koin-core/index.html#_making_your_test_a_koincomponent_with_kointest or https://insert-koin.io/docs/2.0/getting-started/junit-test/ but every time getting NoSuchMethodError. What I am doing wrong?

First I was using already created modules from main package, but there was this error. Then I created modules in test package, but error is still the same.

My code

class ComponentA
class ComponentB(val a: ComponentA)

class SignInTest : KoinTest {

    val componentB : ComponentB by inject()

    @Before
    fun before() {
        startKoin { modules(
            module {
                single { ComponentA() }
                single { ComponentB(get()) }
            }) }
    }

    @Test
    fun test_test() {
        val componentA = get<ComponentA>()

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

    @After
    fun after() {
        stopKoin()
    }

java.lang.NoSuchMethodError: org.koin.core.definition.BeanDefinition.(Lorg/koin/core/qualifier/Qualifier;Lorg/koin/core/qualifier/Qualifier;Lkotlin/reflect/KClass;)V

at net.app.at.features.signin.SignInTest$before$1$1.invoke(SignInTest.kt:79)
at net.app.at.features.signin.SignInTest$before$1$1.invoke(SignInTest.kt:26)
at org.koin.dsl.ModuleKt.module(Module.kt:31)
at org.koin.dsl.ModuleKt.module$default(Module.kt:29)
at net.app.at.features.signin.SignInTest$before$1.invoke(SignInTest.kt:36)
at net.app.at.features.signin.SignInTest$before$1.invoke(SignInTest.kt:26)
at org.koin.core.context.GlobalContextKt.startKoin(GlobalContext.kt:72)
at net.app.at.features.signin.SignInTest.before(SignInTest.kt:35)

Solution

  • Please check versions of Koin libraries you are using.

    I had same problem. It turns out that I had in my build.gradle:

    implementation "org.koin:koin-android:2.0.0-beta-1" 
    

    and a few lines below:

    testImplementation "org.koin:koin-test:2.0.0"
    

    When I set version 2.0.0 in both places - it worked.