Partho Paul
06/30/2022, 11:11 AMclass A(b: B(), c: C())
class B(d: D())
class C()
I want to write test cases of A
and I did the following:
class ATest : KoinTest {
@JvmField
@RegisterExtension
val koinTestExtension = KoinTestExtension.create {
val serviceModules = module {
factory { B(get()) }
}
val daoModules = module {
factory { C() }
}
val httpClient = module {
factory { D() }
}
modules(httpClient, daoModules, serviceModules)
}
@JvmField
@RegisterExtension
val mockProvider = MockProviderExtension.create { clazz ->
mockkClass(clazz)
}
@Test
fun doTest(koin: Koin) = runBlocking {
declareMock<B> {
coEvery { something(any()) } returns Either.Right("response")
}
declareMock<C> {
coEvery { fetchDoc(any()) } returns Either.Right(GetItemResponse {
item = mapOf("key" to AttributeValue.S("value"))
})
}
val a = A(
b = koin.get<B>(),
c = koin.get<C>()
)
val resp = a.doSomething(request)
assertEquals(resp.isLeft(), false)
}
}
Is there a more elegant way of mocking `B`and C
and put them inside A
?