Skip to content

Instantly share code, notes, and snippets.

@Szer
Created June 25, 2022 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Szer/8c6782dc644343c285eed74161efd32c to your computer and use it in GitHub Desktop.
Save Szer/8c6782dc644343c285eed74161efd32c to your computer and use it in GitHub Desktop.
Ktor engine reuse for tests
import io.ktor.config.*
import io.ktor.server.engine.*
import io.ktor.server.testing.*
import org.junit.jupiter.api.extension.*
import org.slf4j.LoggerFactory
class KtorTests(private val app: App) : AfterAllCallback, BeforeAllCallback {
val env = applicationEngineEnvironment {
config = MapApplicationConfig("ktor.deployment.environment" to "test")
log = LoggerFactory.getLogger("ktor.test")
}
val engine by lazy {
val engine = TestApplicationEngine(env) {}
engine.start()
engine
}
fun test(block: TestApplicationEngine.() -> Unit) {
with(engine) { block() }
}
override fun beforeAll(context: ExtensionContext?) {
with(engine) {
with(app) { application.mainModule() }
}
}
override fun afterAll(context: ExtensionContext?) {
engine.stop(0, 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment