Laurent Thiebaud
02/14/2025, 2:59 PMcompagnon object
/ @JvmStatic beforeAll
statement.
Ex:
class MyTest: KoinTest {
@JvmField
@RegisterExtension
val koinTestExtension = KoinTestExtension.create { modules(module { moduleDeclaration }) }
val Module.moduleDeclaration: KoinDefinition<myServiceInstance>
get() = single { myServiceInstance() }
companion object {
@JvmStatic
@BeforeAll
fun beforeAll() {
declare { ADependencyService }
}
}
}
Error is:
Cannot infer type for this parameter. Please specify it explicitly.
Unresolved reference. None of the following candidates is applicable because of a receiver type mismatch: fun <reified T : Any> KoinTest. declare(qualifier: Qualifier? = ..., noinline instance: () -> T): TTo make it compile I must use:
companion object: KoinTest { ...}
However this fails at runtime with:
KoinApplication has not been started
java.lang.IllegalStateException: KoinApplication has not been startedI guess that the compagnon object is instanciated before the class, which is responsible to start koin. Any idea how to make it work? It is a pîty that
beforeAll
cannot be used with koin.