Hello, I’m playing around with `checkModules()` an...
# koin
o
Hello, I’m playing around with
checkModules()
and I got into a situation where I keep getting
NoBeanDefFoundException
. I have three classes, two of which are declared in a module. In test, I provide the third class mock using
withInstance<Outsider>()
. But for some reason,
checkModules()
throws
NoBeanDefFoundException: No definition found for type 'com.test.di.Outsider'. Check your Modules configuration and add missing type and/or qualifier!
. Does anybody know why? I guess it’s related to the parameter which the factory for
First
requires. When I remove the parameter,
checkModules()
says it’s all good. Thanks in advance for any help.
Copy code
data class Outsider(val version: String)
data class First(val outsider: Outsider, val booleanParam: Boolean)
data class Second(val first: First)
Copy code
val testModule = module {
  factory { (booleanParam: Boolean) ->
    First(
      outsider = get(),
      booleanParam = booleanParam
    )
  }

  factory {
    Second(
      first = get { parametersOf(true) }
    )
  }
}
Copy code
class TestModuleTests {
  @get:Rule
  val mockProvider = MockProviderRule.create { clazz ->
    mockkClass(clazz, relaxed = true)
  }

  @Test
  fun checkModules() {
    koinApplication {
      modules(testModule)
      checkModules {
        withInstance<Outsider>()
      }
    }
  }
}
l
It seems to me it's some kind of ordering issue. Is it possible for you to bring
withInstance<Outsider>()
to some point before checkModules()?
My guess is
checkModules
is building the tree before executing your function
o
I don't think that's possible, because
withInstance<T>()
is a function on
ParametersBinding
. So it needs to be called from
checkModules {}
.
I also tried to specify the parameter for
First
in
checkModules()
like the documentation says, but I'm still getting the same error.
Copy code
@Test
fun checkModules() {
    koinApplication {
        modules(testModule)
        checkModules {
            withInstance<Outsider>()
            withParameters<First> { parametersOf(false) }
        }
    }
}
116 Views