文档介绍了依赖注入,但没有真正展示如何实现。
文档还未完成,有许多占位符:http://ktor.io/getting-started.html 我尝试创建主函数使其接受参数(即我的依赖项),但在测试调用
文档还未完成,有许多占位符:http://ktor.io/getting-started.html 我尝试创建主函数使其接受参数(即我的依赖项),但在测试调用
withTestApplication
时失败了。
我查看了应用程序代码,并发现Application接受一个配置对象,但我不知道如何更改该配置对象以注入其中的某些依赖项。package org.jetbrains.ktor.application
/**
* Represents configured and running web application, capable of handling requests
*/
class Application(val environment: ApplicationEnvironment) : ApplicationCallPipeline() {
/**
* Called by host when [Application] is terminated
*/
fun dispose() {
uninstallAllFeatures()
}
}
/**
* Convenience property to access log from application
*/
val Application.log get() = environment.log
在使用withTestApplication
的测试代码中,我有以下类似的内容:
@Test
internal fun myTest() = withTestApplication (Application::myMain)
如果我使用参数调用myMain
(这些参数需要进行模拟和注入),那么上述的withTestApplication
将会失败。
更新:
问题在于在我的请求处理中,我正在使用一个连接到其他外部网络服务并执行一些请求的依赖类。我需要一种方法来注入它,以便在测试中可以stub/mock并根据我的测试用例更改其行为。