07-20-2023, 03:58 PM
The documentation talks about dependency injection but does not really show how it is being done.
Documentation is not completed as well and has a bunch of place holders:
I tried to create my main function in a way that it accepts parameter (which is my dependency) but that failed on the test side when I call `withTestApplication`.
I looked into the application code and saw that Application accepts a configuration object but I have no idea how I can change that configuration object to inject some dependencies inside of it.
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
In the test code using `withTestApplication` I have something similar to the below:
@Test
internal fun myTest() = withTestApplication (Application::myMain)
The above `withTestApplication` would fail if I call `myMain` with parameters (parameters that I need to mock and inject.)
**Update:**
The issue is that in my request handling, I am using a dependency class that connects to other web services outside and does some requests, I need a way to be able to inject this so in my tests I can stub/mock it and change its behavior based on my test cases.
Documentation is not completed as well and has a bunch of place holders:
[To see links please register here]
I tried to create my main function in a way that it accepts parameter (which is my dependency) but that failed on the test side when I call `withTestApplication`.
I looked into the application code and saw that Application accepts a configuration object but I have no idea how I can change that configuration object to inject some dependencies inside of it.
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
In the test code using `withTestApplication` I have something similar to the below:
@Test
internal fun myTest() = withTestApplication (Application::myMain)
The above `withTestApplication` would fail if I call `myMain` with parameters (parameters that I need to mock and inject.)
**Update:**
The issue is that in my request handling, I am using a dependency class that connects to other web services outside and does some requests, I need a way to be able to inject this so in my tests I can stub/mock it and change its behavior based on my test cases.