Ktor: config doesn't load while running ktor-test

Created on 14 Mar 2018  路  1Comment  路  Source: ktorio/ktor

Simple test

@Test fun getTest() = withTestApplication(Application::main){
        with(handleRequest(HttpMethod.Get, "/")) {
            assertEquals(HttpStatusCode.OK, response.status())
        }
    }

Fails because the app is unable to load its properties:

io.ktor.config.ApplicationConfigurationException: Property db.user not found.

    at io.ktor.config.MapApplicationConfig.property(MapApplicationConfig.kt:31)
    at opprt.Data.<init>(Data.kt:18)
    at opprt.AppKt.main(App.kt:36)
    at opprt.ApiTests$getTest$1.invoke(ApiTests.kt:17)
    at opprt.ApiTests$getTest$1.invoke(ApiTests.kt:14)
        ...

at io.ktor.config.MapApplicationConfig.property(MapApplicationConfig.kt:31) - looks like that map config is being loaded, although I have a HoconConfig (application.conf) in my app.

Here's what's happening in my app:

fun Application.main(){

        val dbConfig = environment.config.config("db")
        val dao = Data(dbConfig).migrateDb()

Likely problem is somewhere in TestEngine.kt ... looks like it's overwriting any app level configuration with a fresh test config:

fun createTestEnvironment(configure: ApplicationEngineEnvironmentBuilder.() -> Unit = {}) = applicationEngineEnvironment {
    config = MapApplicationConfig("ktor.deployment.environment" to "test")
    log = LoggerFactory.getLogger("ktor.test")
    configure()
}

I don't see any function that's sending in a relevant configure function in to createTestEnvironment.

Please help resolving the issue.

Most Likely Explanation: I just haven't figured out how to set the configuration up.

Most helpful comment

Can probably be closed. Here's what I had to do to make this work:

Initialize the engine in a companion object with the relevant configuration

class ApiTests {

    companion object {
        val engine = TestApplicationEngine(createTestEnvironment({
            config = HoconApplicationConfig(ConfigFactory.load("application-test.conf"))
        }))

        @BeforeClass @JvmStatic fun setup(){
            logger.debug("Starting application with config ....")
            engine.start(wait = false)
            engine.application.main()
        }


    }

Use with(engine) instead of withTestApplication:

@Test fun testSomething = with(engine){
        with(handleRequest(HttpMethod.Get, uri, {
            addHeader("accept", "application/json")
        })) {
            assertions...
        }
}

>All comments

Can probably be closed. Here's what I had to do to make this work:

Initialize the engine in a companion object with the relevant configuration

class ApiTests {

    companion object {
        val engine = TestApplicationEngine(createTestEnvironment({
            config = HoconApplicationConfig(ConfigFactory.load("application-test.conf"))
        }))

        @BeforeClass @JvmStatic fun setup(){
            logger.debug("Starting application with config ....")
            engine.start(wait = false)
            engine.application.main()
        }


    }

Use with(engine) instead of withTestApplication:

@Test fun testSomething = with(engine){
        with(handleRequest(HttpMethod.Get, uri, {
            addHeader("accept", "application/json")
        })) {
            assertions...
        }
}
Was this page helpful?
0 / 5 - 0 ratings