Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Koin 3.1.0 has breaking changes to checkModules syntax and behavior #1115

Closed
russhwolf opened this issue Jun 14, 2021 · 2 comments
Closed

Koin 3.1.0 has breaking changes to checkModules syntax and behavior #1115

russhwolf opened this issue Jun 14, 2021 · 2 comments
Milestone

Comments

@russhwolf
Copy link
Contributor

The following is a valid test on both JVM and Native in Koin version 3.0.2 using the koin-core and koin-test modules.

class Box(val value: String)

class KoinModulesTest {
    @Test
    fun checkModulesTest() {
        val koinApplication = startKoin {
            modules(module {
                single { Box(get()) }
            })
        }

        koinApplication.checkModules {
            create<Box> { parametersOf("foo") }
        }
    }

    @AfterTest
    fun tearDown() {
        stopKoin()
    }
}

In Koin 3.1.0, the checkModules() extension function which takes a lambda has been removed, breaking compatibility with existing code. What follows is my attempts to understand the new API.


One can try to migrate the checkModules() call like so:

koinApplication.checkModules(defaultValues = mapOf(String::class to "foo"))

This fails with the following message:

org.koin.core.error.BrokenDefinitionException: Definition is broken: [Singleton:'Box'] 
- due to error: org.koin.core.error.NoBeanDefFoundException: No definition found for class:'java.lang.String'. Check your definitions!

I guess defaultValues doesn't work how I might expect. What is the real replacement for create<Box> { parametersOf("foo") }?

This isn't exactly what I want, but let's try putting the parameter directly in the graph.

        val koinApplication = startKoin {
            modules(module {
                single { Box(get()) }
                single { "foo" }
            })
        }

        koinApplication.checkModules()

This fails with a missing MockProvider:

Definition is broken: [Singleton:'Box'] 
- due to error: java.lang.IllegalStateException: Missing MockProvider. Please use MockProvider.register() to register a new mock provider

But why do I need a MockProvider when nothing in the graph needs to be mocked?

Ok, so let's try it this way:

        val koinApplication = startKoin {
            modules(module {
                single { Box(get()) }
                single { "foo" }
            })
        }

        MockProvider.register {
            when (it) {
                String::class -> "bar"
                else -> error("unknown type!")
            }
        }
        koinApplication.checkModules()

This finally works on JVM, but I remain confused. It fails if single { "foo" } is omitted but actually creates a Box using "bar". I don't understand why it uses a mock instead of the value already on the graph. Furthermore, using MockProvider in that way obviously won't scale very well. I guess it's intended to plug in something like Mockk or Mockito but that's only possible on JVM.

Also, that test doesn't work on Native, but fails with

kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen org.koin.test.mock.MockProvider@38510278

In fact, it's impossible to register a mock provider on Native because this involves mutating state in a top-level object which is not allowed by the current memory model.

All told, I find these API breakages confusing and I don't understand how to migrate my koin tests using checkModules() from 3.0.2 to 3.1.0.

@headsvk
Copy link

headsvk commented Jun 14, 2021

I'm also getting this error for some classes that are part of my graph. I's not clear at all why they should be mocked, I run tests on the emulator.

@arnaudgiuliani
Copy link
Member

checkModules API has been reverted in 3.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants