Andrey V
01/31/2025, 6:12 AMfun 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
@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 usingAleksei Tirman [JB]
01/31/2025, 7:43 AMhttpMain
method:
@Test
fun test() = testApplication {
application {
httpMain(config)
}
// ... The code of the test
}
Andrey V
02/03/2025, 7:59 AMhttpMain(config).invoke(this)
)
But that got things working, thanks