With the ktor 2.0 `testApplication` block, I’m try...
# ktor
p
With the ktor 2.0
testApplication
block, I’m trying to test some lifecycle hooks for a custom plugin. Is there a nice way to ensure the application environment is started than making a client call? I currently have something like this:
Copy code
private suspend fun ApplicationTestBuilder.ensureStarted() = client.get("/") { expectSuccess = false }

class MyTest : FunSpec({
    test("plugin works correctly") {
        testApplication {
            install(MyCustomPlugin) { ... }

            ensureStarted()

            // test the plugin
        }
    }
})
a
Could you please describe why do you need to test it? The application environment should be started in a test.
p
The plugin starts a
Job
on
ApplicationStarted
event, which won’t fire until the application environment is started. With the
testApplication
construct, there’s no way to explicitly start the application beyond as a side effect of the first call through the provided (or a constructed)
client
. I need to test that the
Job
starts and operates correctly.
a
What is the use for this job?
p
It sets up kafka consumers, currently. However this problem would apply to any plugin that would like to handle the application lifecycle events from Ktor.
a
Do you mean that you can’t test your essential plugin logic without first making a request to start an application environment?
p
Yes, exactly that. The only way to start the test application environment is to make a request. I could potentially use the
TestApplication
builder and manually
start
and
stop
the application, however this doesn’t allow to accessing the
client
within the builder, unlike
testApplication
- which is needed to access any
externalServices
defined in the test application.
a
Could you please file an issue about this problem?