0

我想对使用 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 注入呢?

提前致谢

4

2 回答 2

1

如果您尝试设置用于测试/模拟的代码而不是尝试研究 Koin 的功能,那么我这样做的方式就是将事情分解,例如:

fun Application.module(testing: Boolean = false) {

    val todoService: TodoService by inject()
    moduleWithDependencies(eventService)
}

fun Application.moduleWithDependencies(eventService: EventService) {
    install(Routing) {
        eventApi(eventService)
    }
}

eventApi 有路线,然后我可以独立测试

这里有一个例子(不是很完整)https://github.com/kevinrjones/koodoo

于 2019-05-08T18:11:36.183 回答
-1

问题是你在里面开始 Koin Application.module()

最好在调用之前Application.module()启动 Koin 。所以你可以在生产和测试环境中初始化不同的依赖。

这是如何启动生产环境:

fun main(args: Array<String>) {
    val appEnvironment = commandLineEnvironment(args)
    startKoin { modules(initAppModule(appEnvironment.config)) }
    embeddedServer(Netty, appEnvironment).start(true)
}

这是测试环境:

fun testApp(test: TestApplicationEngine.() -> Unit) {
    withApplication(createTestEnvironment {
        config = HoconApplicationConfig(ConfigFactory.load("application.conf"))
        startKoin { modules(initTestAppModule(config)) }
    }, {
    }, test)

    val koin = KoinContextHandler.get()
    stopKoin() // Stop koin after test
}

请参阅此处的示例:https ://github.com/comm1x/ktor-boot/blob/master/src/main.kt

在那里:https ://github.com/comm1x/ktor-boot/blob/master/test/common/common.kt

于 2020-06-25T00:11:12.217 回答