-
-
Save Szer/8c6782dc644343c285eed74161efd32c to your computer and use it in GitHub Desktop.
Ktor engine reuse for tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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