Я хочу сделать тест для маршрута Ktor, который использует Koin для внедрения сервиса.Я пытаюсь высмеять службу, используемую маршрутом Ktor в моем тесте.
Вот файл Ktor Application.kt
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
install(Locations) {
}
// Suppressed code
installKoin(listOf(dependencyInjectionModule))
routing {
val eventService by inject<EventService>()
get("/issues/{issueNumber}/events") {
val issue = call.parameters["issueNumber"]
val issueNumber = parseInt(issue)
call.respond(eventService.getEventsByIssueNumber(issueNumber))
}
}
// Suppressed code
}
Здесь работает Коин, проблема в том, что мне нужно издеваться вместо использования Коина.
Вот файл AplicationTest.kt
class ApplicationTest {
private val eventDtoList = listOfNotNull(
EventDto(1, "opened", "2019-03-24T21:40:18Z", 1 ),
EventDto(2, "closed", "2019-03-28T21:40:18Z", 1 )
)
@Test
fun testGettingEventsByExistingIssueNumber() {
withTestApplication({ module(testing = true) }) {
stopKoin()
handleRequest(HttpMethod.Get, "/issues/1/events").apply {
val eventService:EventService = mock {
onBlocking { getEventsByIssueNumber(any()) } doReturn eventDtoList
}
val resultList = Gson().fromJson(response.content, Array<EventDto>::class.java).asList()
assertEquals(response.status(), HttpStatusCode.OK)
assertThat(resultList).hasSize(2)
}
}
}
}
Я хочу смоделировать список событий, который возвращается службой, но я, когда я попадаю на эту строку:
withTestApplication({ module(testing = true) })
Я получаю это исключение:
2019-05-08 11:51:40:881 (KOIN)::[e] Error while resolving instance for class 'com.service.EventService' - error: org.koin.error.NoBeanDefFoundException: No compatible definition found for type 'EventService'. Check your module definition
org.koin.error.NoBeanDefFoundException: No compatible definition found for type 'EventService'. Check your module definition
at org.koin.core.bean.BeanRegistry.retrieveDefinition(BeanRegistry.kt:113)
at org.koin.core.instance.InstanceRegistry$proceedResolution$$inlined$synchronized$lambda$1.invoke(InstanceRegistry.kt:87)
at org.koin.core.instance.InstanceRegistry$proceedResolution$$inlined$synchronized$lambda$1.invoke(InstanceRegistry.kt:36)
at org.koin.core.time.DurationKt.measureDuration(Duration.kt:8)
at org.koin.core.instance.InstanceRegistry.proceedResolution(InstanceRegistry.kt:84)
at org.koin.core.instance.InstanceRegistry.resolve(InstanceRegistry.kt:63)
at org.koin.core.instance.InstanceRegistry.resolve$default(InstanceRegistry.kt:48)
at com.jaya.octovevent.ApplicationKt$module$4$$special$$inlined$inject$1.invoke(KtorRoutingExt.kt:112)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.jaya.octovevent.ApplicationKt$module$4$2.invokeSuspend(Application.kt:76)
at com.ApplicationKt$module$4$2.invoke(Application.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
at io.ktor.features.StatusPages$interceptCall$2.invokeSuspend(StatusPages.kt:94)
at io.ktor.features.StatusPages$interceptCall$2.invoke(StatusPages.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:91)
at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:180)
at io.ktor.features.StatusPages.interceptCall(StatusPages.kt:93)
at io.ktor.features.StatusPages$Feature$install$2.invokeSuspend(StatusPages.kt:133)
at io.ktor.features.StatusPages$Feature$install$2.invoke(StatusPages.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:157)
at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:23)
at io.ktor.routing.Routing.executeResult(Routing.kt:148)
at io.ktor.routing.Routing.interceptor(Routing.kt:29)
at io.ktor.routing.Routing$Feature$install$1.invokeSuspend(Routing.kt:93)
at io.ktor.routing.Routing$Feature$install$1.invoke(Routing.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
at io.ktor.features.ContentNegotiation$Feature$install$1.invokeSuspend(ContentNegotiation.kt:63)
at io.ktor.features.ContentNegotiation$Feature$install$1.invoke(ContentNegotiation.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:157)
at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:23)
at io.ktor.server.testing.TestApplicationEngine$2.invokeSuspend(TestApplicationEngine.kt:265)
at io.ktor.server.testing.TestApplicationEngine$2.invoke(TestApplicationEngine.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:157)
at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:23)
at io.ktor.server.testing.TestApplicationEngine$handleRequest$pipelineJob$1.invokeSuspend(TestApplicationEngine.kt:263)
at |b|b|b(Coroutine boundary.|b(|b)
at io.ktor.features.StatusPages.interceptCall(StatusPages.kt:93)
at io.ktor.features.StatusPages$Feature$install$2.invokeSuspend(StatusPages.kt:133)
at io.ktor.routing.Routing.executeResult(Routing.kt:148)
at io.ktor.routing.Routing$Feature$install$1.invokeSuspend(Routing.kt:93)
at io.ktor.features.ContentNegotiation$Feature$install$1.invokeSuspend(ContentNegotiation.kt:63)
at io.ktor.server.testing.TestApplicationEngine$2.invokeSuspend(TestApplicationEngine.kt:265)
at io.ktor.server.testing.TestApplicationEngine$handleRequest$pipelineJob$1.invokeSuspend(TestApplicationEngine.kt:263)
at kotlinx.coroutines.DeferredCoroutine.await$suspendImpl(Builders.common.kt:98)
at io.ktor.server.testing.TestApplicationEngine$handleRequest$1.invokeSuspend(TestApplicationEngine.kt:129)
Caused by: org.koin.error.NoBeanDefFoundException: No compatible definition found for type 'EventService'. Check your module definition
at org.koin.core.bean.BeanRegistry.retrieveDefinition(BeanRegistry.kt:113)
at org.koin.core.instance.InstanceRegistry$proceedResolution$$inlined$synchronized$lambda$1.invoke(InstanceRegistry.kt:87)
at org.koin.core.instance.InstanceRegistry$proceedResolution$$inlined$synchronized$lambda$1.invoke(InstanceRegistry.kt:36)
at org.koin.core.time.DurationKt.measureDuration(Duration.kt:8)
at org.koin.core.instance.InstanceRegistry.proceedResolution(InstanceRegistry.kt:84)
at org.koin.core.instance.InstanceRegistry.resolve(InstanceRegistry.kt:63)
at org.koin.core.instance.InstanceRegistry.resolve$default(InstanceRegistry.kt:48)
at com.ApplicationKt$module$4$$special$$inlined$inject$1.invoke(KtorRoutingExt.kt:112)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.ApplicationKt$module$4$2.invokeSuspend(Application.kt:76)
at com.ApplicationKt$module$4$2.invoke(Application.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
at io.ktor.features.StatusPages$interceptCall$2.invokeSuspend(StatusPages.kt:94)
at io.ktor.features.StatusPages$interceptCall$2.invoke(StatusPages.kt)
at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:91)
at kotlinx.coroutines.CoroutineScopeKt.coroutineScope(CoroutineScope.kt:180)
at io.ktor.features.StatusPages.interceptCall(StatusPages.kt:93)
at io.ktor.features.StatusPages$Feature$install$2.invokeSuspend(StatusPages.kt:133)
at io.ktor.features.StatusPages$Feature$install$2.invoke(StatusPages.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:157)
at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:23)
at io.ktor.routing.Routing.executeResult(Routing.kt:148)
at io.ktor.routing.Routing.interceptor(Routing.kt:29)
at io.ktor.routing.Routing$Feature$install$1.invokeSuspend(Routing.kt:93)
at io.ktor.routing.Routing$Feature$install$1.invoke(Routing.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
at io.ktor.features.ContentNegotiation$Feature$install$1.invokeSuspend(ContentNegotiation.kt:63)
at io.ktor.features.ContentNegotiation$Feature$install$1.invoke(ContentNegotiation.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:157)
at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:23)
at io.ktor.server.testing.TestApplicationEngine$2.invokeSuspend(TestApplicationEngine.kt:265)
at io.ktor.server.testing.TestApplicationEngine$2.invoke(TestApplicationEngine.kt)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(PipelineContext.kt:278)
at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(PipelineContext.kt:63)
at io.ktor.util.pipeline.SuspendFunctionGun.proceed(PipelineContext.kt:137)
at io.ktor.util.pipeline.SuspendFunctionGun.execute(PipelineContext.kt:157)
at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:23)
at io.ktor.server.testing.TestApplicationEngine$handleRequest$pipelineJob$1.invokeSuspend(TestApplicationEngine.kt:263)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:238)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)
Я знаю, что Koin не может найти реализацию EventService, потому что я остановил ее (Koin).Так как я могу заменить инъекцию Коина своим поддельным сервисом?
Заранее спасибо