In a project, I do environment setup along the lin...
# ktor
a
In a project, I do environment setup along the lines of
Copy code
fun httpMain(config: AppConfig): (Application)->Unit {
    fun Application.httpMain() {
        install(...) { ... }
        configureRouting(repo = config.repo)
    }
    return Applciation::httpMain
}

fun main() {
    val config = AppConfig.config
    embeddedServer(...,
        module = httpMain(config)
    ).start(wait = true)
}
with junit5 unit tests of repo, i can do something like
Copy code
@BeforeAll
fun prepareDb() {
    val db = Database.connect(HikariDataSource()).apply { jdbcUrl = "jdbc:h2:testdb;DB_CLOSE_DELAY=-1" }
    prepareDatabase(db)
    /* lateinit var in class */ repo = Repo(db, ...)
    ...
}
where
prepareDatabase
executes a bunch of inserts. What would be the cleanest way of getting
testApplication
working to test the http bits so it acts on a temporary h2 db like the repo unit tests do? The test environment bits in the official docs feel a bit too tightly coupled re config loading compared to the approach I'm using
a
You can configure the test server in a similar way you configure the embedded server by passing the config being set up with the temporal h2 database to the
httpMain
method:
Copy code
@Test
fun test() = testApplication {
    application {
        httpMain(config)
    }

    // ... The code of the test
}
thank you color 1
a
(One small detail: it turned out that the correct form was
httpMain(config).invoke(this)
) But that got things working, thanks