Best Kotest code snippet using io.kotest.core.extensions.MountableExtension.mount
JdbcTestContainerExtension.kt
Source:JdbcTestContainerExtension.kt
...21 *22 * This extension will create a pooled [HikariDataSource] attached to the database and23 * return that to the user as the materialized value.24 *25 * The pool can be configured in the mount configure method.26 *27 * Note: This extension requires Kotest 5.0+28 *29 * @param container the specific test container type30 * @param lifecycleMode determines how the container should be reset between tests31 *32 * @since 1.1.033 */34class JdbcTestContainerExtension(35 private val container: JdbcDatabaseContainer<Nothing>,36 private val lifecycleMode: LifecycleMode = LifecycleMode.Spec,37) : MountableExtension<TestContainerHikariConfig, DataSource>, AfterSpecListener, TestListener {38 private val ds = SettableDataSource(null)39 private var configure: TestContainerHikariConfig.() -> Unit = {}40 override fun mount(configure: TestContainerHikariConfig.() -> Unit): DataSource {41 this.configure = configure42 if (lifecycleMode == LifecycleMode.Spec) {43 container.start()44 ds.setDataSource(createDataSource())45 }46 return ds47 }48 private fun createDataSource(): HikariDataSource {49 val config = TestContainerHikariConfig()50 config.jdbcUrl = container.jdbcUrl51 config.username = container.username52 config.password = container.password53 config.configure()54 val ds = HikariDataSource(config)...
SharedJdbcDatabaseContainerExtension.kt
Source:SharedJdbcDatabaseContainerExtension.kt
...52 BeforeSpecListener,53 AfterTestListener,54 AfterSpecListener {55 private var ds: HikariDataSource? = null56 override fun mount(configure: TestContainerHikariConfig.() -> Unit): HikariDataSource {57 if (!container.isRunning) {58 container.start()59 ds = createDataSource().apply(afterStart)60 }61 return ds ?: error("DataSource was not initialized")62 }63 override suspend fun afterProject() {64 if (container.isRunning) container.stop()65 }66 override suspend fun beforeTest(testCase: TestCase) {67 beforeTest(ds ?: error("DataSource was not initialized"))68 }69 override suspend fun afterTest(testCase: TestCase, result: TestResult) {70 afterTest(ds ?: error("DataSource was not initialized"))...
SharedTestContainerExtension.kt
Source:SharedTestContainerExtension.kt
...65 configure66 ) { this }67 }68 }69 override fun mount(configure: T.() -> Unit): U {70 if (!container.isRunning) {71 container.start()72 configure(container)73 this@SharedTestContainerExtension.configure(container)74 }75 return this@SharedTestContainerExtension.mapper(container)76 }77 override suspend fun afterProject() {78 if (container.isRunning) container.stop()79 }80 override suspend fun beforeTest(testCase: TestCase) {81 beforeTest(container)82 }83 override suspend fun afterTest(testCase: TestCase, result: TestResult) {...
TestContainerExtension.kt
Source:TestContainerExtension.kt
...21 TestContainerExtension(GenericContainer<Nothing>(name))22 operator fun invoke(name: String, lifecycleMode: LifecycleMode): TestContainerExtension<GenericContainer<Nothing>> =23 TestContainerExtension(GenericContainer<Nothing>(name), lifecycleMode)24 }25 override fun mount(configure: T.() -> Unit): T {26 (container as T).configure()27 if (lifecycleMode == LifecycleMode.Spec) {28 container.start()29 }30 return container31 }32 override suspend fun afterSpec(spec: Spec) {33 if (container.isRunning) {34 withContext(Dispatchers.IO) {35 stop()36 }37 }38 }39 override suspend fun beforeAny(testCase: TestCase) {...
MountableExtensionTest.kt
Source:MountableExtensionTest.kt
...6import io.kotest.core.spec.style.FunSpec7import io.kotest.matchers.shouldBe8import java.util.concurrent.atomic.AtomicBoolean9class MountableExtensionTest : FunSpec() {10 private val mountable = MyMountable()11 private val control = install(mountable) {12 a = "bar"13 }14 init {15 test("mountable extensions should invoke configuration block") {16 control.a shouldBe "bar"17 }18 test("mountable extensions should be installed as regular extensions") {19 mountable.before.get() shouldBe true20 }21 }22}23data class Config(var a: String)24class MyMountable : MountableExtension<Config, Config>, BeforeSpecListener {25 val before = AtomicBoolean(false)26 override suspend fun beforeSpec(spec: Spec) {27 before.set(true)28 }29 override fun mount(configure: (Config) -> Unit): Config {30 val config = Config("foo")31 configure(config)32 return config33 }34}...
MountableExtension.kt
Source:MountableExtension.kt
...21 *22 */23interface MountableExtension<CONFIG, MATERIALIZED> : Extension {24 // cannot be suspending as it is invoked by install that is used in constructors25 fun mount(configure: CONFIG.() -> Unit): MATERIALIZED26}27// cannot be suspending as it is used in constructors28fun <CONFIG, MATERIALIZED> Spec.install(29 mountable: MountableExtension<CONFIG, MATERIALIZED>,30 configure: CONFIG.() -> Unit = {}31): MATERIALIZED {32 extensions(mountable)33 return mountable.mount(configure)34}...
mount
Using AI Code Generation
1val extension = MountableExtension { mount ->2 }3}4val extension = Extension { mount ->5 }6}7val listener = MountableListener { mount ->8 }9}10val listener = Listener { mount ->11 }12}13val spec = MountableSpec { mount ->14 }15}16val spec = Spec { mount ->17 }18}19val test = MountableTest { mount ->20 }21}22val test = Test { mount ->23 }24}25val testCase = MountableTestCase { mount ->26 }27}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!