我想对使用 Koin 注入服务的 Ktor 路由进行测试。我正在努力模拟测试中 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
}
Koin 在上面工作,问题是当我需要模拟而不是使用 Koin 时。
这是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)
}
}
}
}
我想模拟服务返回的 eventList,但是当我到达这一行时:
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)。那么如何用我的模拟服务替换 Koin 注入呢?
提前致谢