https://kotlinlang.org logo
#ktor
Title
# ktor
a

Antonio Acuña Prieto

03/12/2024, 8:43 PM
Hi, Something weird is happening, is the test is executed like this:
Copy code
TestApplication {
    environment {
        config = ApplicationConfig("application.conf")
    }
}
Modules defined in
application.conf
load fine, but when I try to override any parameter of the configuration using:
Copy code
TestApplication {
    environment {
        config = ApplicationConfig("application.conf").mergeWith(
            MapApplicationConfig(
                "database.host" to "whatever",
            )
        )
    }
}
Then the modules are loaded two times. Version 3.0.0-beta-1
a

Aleksei Tirman [JB]

03/13/2024, 8:11 AM
I cannot reproduce your problem with the following code:
Copy code
@Test
fun test() = testApplication {
    environment {
        config = ApplicationConfig("application.conf").mergeWith(
            MapApplicationConfig(
                "database.host" to "whatever",
            )
        )
    }

    client.get("/")
}
application.conf:
Copy code
ktor {
    deployment {
        port = 9090
    }

    application {
        modules = [ EngineMainKt.main ]
    }
}

database {
    host = localhost
}
EngineMain.kt:
Copy code
fun Application.main() {
    println(environment.config.property("database.host").getString())
}
a

Antonio Acuña Prieto

03/13/2024, 9:53 AM
I’ll prepare a report to reproduce it
The problem is different than what I thought, I can explain using this example:
Copy code
object Ktor3App {
    suspend fun beforeSpec() {
        httpClient.get("/")
    }

    fun afterSpec() {}

    fun beforeTest() {}

    fun afterTest() {}

    val inMemorySpanExporter: InMemorySpanExporter
    val app: TestApplication
    val httpClient: HttpClient

    init {
        inMemorySpanExporter = InMemorySpanExporter.create()
        app = TestApplication {
            environment {
                config = ApplicationConfig("src/test/resources/application-test.conf")
            }
        }
        httpClient = app.createClient { }
    }
}
Now I know that the testApplication server will start when the first request is made, but I need to start the server after that happens so my solution is doing a get request after the tests run. If I replace this request in
beforeSpec()
with
app.start()
it will load the modules there and then it will load then on the first client http request. Is this correct? I think it should check if the app is running, server started and modules loaded to not load them again on the first request but no strong opinion, hopefully there is a cleaner way to do it.
a

Aleksei Tirman [JB]

03/14/2024, 7:32 AM
If you manually create and start a
TestApplication
then you don't need to use the
testApplication
function.
a

Antonio Acuña Prieto

03/14/2024, 2:33 PM
I’m not using the function, just using TestApplication