Best Spek code snippet using org.spekframework.spek2.lifecycle.LifecycleListener
Collectors.kt
Source:Collectors.kt
1package org.spekframework.spek2.runtime2import org.spekframework.spek2.dsl.*3import org.spekframework.spek2.lifecycle.CachingMode4import org.spekframework.spek2.lifecycle.LifecycleListener5import org.spekframework.spek2.lifecycle.MemoizedValue6import org.spekframework.spek2.runtime.lifecycle.LifecycleManager7import org.spekframework.spek2.runtime.lifecycle.MemoizedValueCreator8import org.spekframework.spek2.runtime.lifecycle.MemoizedValueReader9import org.spekframework.spek2.runtime.scope.*10class Collector(11 val root: GroupScopeImpl,12 private val lifecycleManager: LifecycleManager,13 private val fixtures: FixturesAdapter,14 override val defaultCachingMode: CachingMode,15 override var defaultTimeout: Long16) : Root {17 private val ids = linkedMapOf<String, Int>()18 override fun <T> memoized(mode: CachingMode, factory: () -> T): MemoizedValue<T> = memoized(mode, factory) { }19 override fun <T> memoized(mode: CachingMode, factory: () -> T, destructor: (T) -> Unit): MemoizedValue<T> {20 return MemoizedValueCreator(21 root,22 mode,23 factory,24 destructor25 )26 }27 override fun <T> memoized(): MemoizedValue<T> {28 return MemoizedValueReader(root)29 }30 override fun registerListener(listener: LifecycleListener) {31 lifecycleManager.addListener(listener)32 }33 override fun group(description: String, skip: Skip, defaultCachingMode: CachingMode, preserveExecutionOrder: Boolean, failFast: Boolean, body: GroupBody.() -> Unit) {34 val group = GroupScopeImpl(35 idFor(description),36 root.path.resolve(description),37 root,38 skip,39 lifecycleManager,40 preserveExecutionOrder,41 failFast42 )43 root.addChild(group)44 val cachingMode = if (defaultCachingMode == CachingMode.INHERIT) {...
SpekLifecycleUtils.kt
Source:SpekLifecycleUtils.kt
...4import org.spekframework.spek2.dsl.Root5import org.spekframework.spek2.dsl.Skip6import org.spekframework.spek2.dsl.TestBody7import org.spekframework.spek2.lifecycle.CachingMode8import org.spekframework.spek2.lifecycle.LifecycleListener9interface ExecutionResultAwareGroupBody : GroupBody {10 fun afterEachSuccessfulTest(handler: TestBody.() -> Unit)11 fun afterEachFailedTest(handler: TestBody.(cause: Throwable) -> Unit)12}13interface ExecutionResultAwareRoot : Root, ExecutionResultAwareGroupBody14fun GroupBody.afterEachSuccessfulTest(handler: TestBody.() -> Unit) =15 (this as ExecutionResultAwareGroupBody).afterEachSuccessfulTest(handler)16fun GroupBody.afterEachFailedTest(handler: TestBody.(cause: Throwable) -> Unit) =17 (this as ExecutionResultAwareGroupBody).afterEachFailedTest(handler)18fun GroupBody.executionResultAware(block: ExecutionResultAwareGroupBody.() -> Unit) {19 val adapter = (this as? ExecutionResultAwareGroupBody) ?: ExecutionResultAwareGroupBodyAdapter(this)20 adapter.block()21}22fun Root.executionResultAware(block: ExecutionResultAwareRoot.() -> Unit) {23 val adapter = (this as? ExecutionResultAwareRoot) ?: ExecutionResultAwareRootAdapter(this)24 adapter.block()25}26fun executionResultAware(root: ExecutionResultAwareRoot.() -> Unit): Root.() -> Unit = {27 this.executionResultAware(root)28}29abstract class ExecutionResultAwareSpek(root: ExecutionResultAwareRoot.() -> Unit) : Spek(executionResultAware(root))30private class ExecutionResultAwareGroupBodyAdapter(31 private val delegate: GroupBody32) : ExecutionResultAwareGroupBody,33 GroupBody by delegate {34 private val afterEachSuccessfulTestHandlers = mutableListOf<TestBody.() -> Unit>()35 private val afterEachFailedTestHandlers = mutableListOf<TestBody.(Throwable) -> Unit>()36 override fun afterEachSuccessfulTest(handler: TestBody.() -> Unit) {37 afterEachSuccessfulTestHandlers.add(handler)38 }39 override fun afterEachFailedTest(handler: TestBody.(cause: Throwable) -> Unit) {40 afterEachFailedTestHandlers.add(handler)41 }42 override fun group(43 description: String,44 skip: Skip,45 defaultCachingMode: CachingMode,46 preserveExecutionOrder: Boolean,47 failFast: Boolean,48 body: GroupBody.() -> Unit49 ) {50 delegate.group(description, skip, defaultCachingMode, preserveExecutionOrder, failFast) {51 ExecutionResultAwareGroupBodyAdapter(this).body()52 }53 }54 override fun test(description: String, skip: Skip, timeout: Long, body: TestBody.() -> Unit) {55 delegate.test(description, skip, timeout) {56 try {57 body()58 this@ExecutionResultAwareGroupBodyAdapter.afterEachSuccessfulTestHandlers.forEach { handler ->59 this.handler()60 }61 } catch (e: Throwable) {62 this@ExecutionResultAwareGroupBodyAdapter.afterEachFailedTestHandlers.forEach { handler ->63 this.handler(e)64 }65 throw e66 }67 }68 }69}70private class ExecutionResultAwareRootAdapter(71 private val delegate: Root72) : ExecutionResultAwareGroupBody by ExecutionResultAwareGroupBodyAdapter(delegate),73 ExecutionResultAwareRoot {74 override fun registerListener(listener: LifecycleListener) =75 delegate.registerListener(listener)76}...
ForgeLifecycleListener.kt
Source:ForgeLifecycleListener.kt
2import fr.xgouchet.elmyr.Forge3import kotlin.reflect.full.memberProperties4import org.spekframework.spek2.lifecycle.ExecutionResult5import org.spekframework.spek2.lifecycle.GroupScope6import org.spekframework.spek2.lifecycle.LifecycleListener7import org.spekframework.spek2.lifecycle.Scope8import org.spekframework.spek2.lifecycle.TestScope9/**10 * A Spek [LifecycleListener] used to synchronize your [Forge] instance with Spek.11 * @property forge the [Forge] instance to keep synced with Spek scopes12 * @property seeds the seeds map (provide a specific seed for each scope in your Spek class)13 */14class ForgeLifecycleListener(15 private val forge: Forge,16 private val seeds: Map<String, Long>17) : LifecycleListener {18 private val usedSeeds: MutableMap<String, Long> = mutableMapOf()19 // region LifecycleListener20 /** @inheritdoc */21 override fun beforeExecuteGroup(group: GroupScope) {22 super.beforeExecuteGroup(group)23 beforeExecuteScope(group)24 }25 /** @inheritdoc */26 override fun beforeExecuteTest(test: TestScope) {27 super.beforeExecuteTest(test)28 beforeExecuteScope(test)29 }30 /** @inheritdoc */31 override fun afterExecuteTest(test: TestScope, result: ExecutionResult) {32 super.afterExecuteTest(test, result)33 if (result is ExecutionResult.Failure) {...
MemoizedValueAdapter.kt
Source:MemoizedValueAdapter.kt
1package org.spekframework.spek2.runtime.lifecycle2import org.spekframework.spek2.lifecycle.GroupScope3import org.spekframework.spek2.lifecycle.LifecycleListener4import org.spekframework.spek2.lifecycle.TestScope5import org.spekframework.spek2.runtime.scope.ScopeImpl6import kotlin.properties.ReadOnlyProperty7import kotlin.reflect.KProperty8sealed class MemoizedValueAdapter<T>(9 val factory: () -> T,10 val destructor: (T) -> Unit11) : ReadOnlyProperty<Any?, T>, LifecycleListener {12 protected sealed class Cached<out T> {13 object Empty : Cached<Nothing>()14 data class Value<out T>(val value: T) : Cached<T>()15 }16 protected var cached: Cached<T> = Cached.Empty17 override fun getValue(thisRef: Any?, property: KProperty<*>) = get()18 fun get(): T {19 val cached = this.cached20 return when (cached) {21 Cached.Empty -> {22 val newCached = Cached.Value(factory())23 this.cached = newCached24 newCached.value25 }...
dsl.kt
Source:dsl.kt
1package org.spekframework.spek2.dsl2import org.spekframework.spek2.lifecycle.CachingMode3import org.spekframework.spek2.lifecycle.LifecycleListener4import org.spekframework.spek2.lifecycle.MemoizedValue5import org.spekframework.spek2.meta.*6sealed class Skip {7 class Yes(val reason: String? = null) : Skip()8 object No : Skip()9}10@SpekDsl11interface Root : GroupBody {12 fun registerListener(listener: LifecycleListener)13}14@SpekDsl15interface GroupBody : LifecycleAware, TestContainer {16 @Synonym(type = SynonymType.GROUP)17 @Descriptions(Description(DescriptionLocation.VALUE_PARAMETER, 0))18 fun group(description: String, skip: Skip = Skip.No, defaultCachingMode: CachingMode = CachingMode.INHERIT, preserveExecutionOrder: Boolean = false, failFast: Boolean = false, body: GroupBody.() -> Unit)19}20@SpekDsl21interface LifecycleAware : ScopeBody {22 val defaultCachingMode: CachingMode23 fun <T> memoized(mode: CachingMode = defaultCachingMode, factory: () -> T): MemoizedValue<T>24 fun <T> memoized(mode: CachingMode = defaultCachingMode, factory: () -> T, destructor: (T) -> Unit): MemoizedValue<T>25 fun beforeEachTest(callback: () -> Unit)26 fun afterEachTest(callback: () -> Unit)...
SpekTestFilesAdapter.kt
Source:SpekTestFilesAdapter.kt
1package de.joshuagleitze.testfiles.spek2import de.joshuagleitze.testfiles.DefaultTestFiles3import org.spekframework.spek2.lifecycle.ExecutionResult4import org.spekframework.spek2.lifecycle.GroupScope5import org.spekframework.spek2.lifecycle.LifecycleListener6import org.spekframework.spek2.lifecycle.Scope7import org.spekframework.spek2.lifecycle.TestScope8import org.spekframework.spek2.runtime.scope.ScopeImpl9internal class SpekTestFilesAdapter internal constructor(private val testFiles: DefaultTestFiles): LifecycleListener {10 override fun beforeExecuteGroup(group: GroupScope) = testFiles.enterScope(nameOf(group))11 override fun beforeExecuteTest(test: TestScope) = testFiles.enterScope(nameOf(test))12 override fun afterExecuteGroup(group: GroupScope, result: ExecutionResult) = testFiles.leaveScope(convert(result))13 override fun afterExecuteTest(test: TestScope, result: ExecutionResult) = testFiles.leaveScope(convert(result))14 private fun nameOf(scope: Scope) = when (scope) {15 is ScopeImpl -> scope.id.name16 is GroupScope -> "unknown group"17 is TestScope -> "unknown test"18 else -> "unknown scope"19 }20 private fun convert(result: ExecutionResult) = when (result) {21 is ExecutionResult.Success -> DefaultTestFiles.ScopeResult.Success22 is ExecutionResult.Failure -> DefaultTestFiles.ScopeResult.Failure23 }...
SpringContext.kt
Source:SpringContext.kt
...5import org.springframework.test.context.TestContext6import org.springframework.test.context.TestContextManager7import kotlin.reflect.KClass8class SpringContext internal constructor(testContextManager: TestContextManager,9 val root: Root): LifecycleListener {10 val testContext: TestContext by lazy { testContextManager.testContext }11 inline fun <reified T: Any> inject(): T {12 val injected by root.memoized {13 testContext.applicationContext.getBean(T::class.java)14 }15 return injected16 }17 override fun afterExecuteTest(test: TestScope) {18 testContext.markApplicationContextDirty(DirtiesContext.HierarchyMode.EXHAUSTIVE)19 }20}21fun Root.createContext(spek: KClass<*>): SpringContext {22 return SpringContext(TestContextManager(spek.java), this@createContext).apply {23 registerListener(this)...
LifecycleManager.kt
Source:LifecycleManager.kt
1package org.spekframework.spek2.runtime.lifecycle2import org.spekframework.spek2.lifecycle.GroupScope3import org.spekframework.spek2.lifecycle.LifecycleListener4import org.spekframework.spek2.lifecycle.TestScope5class LifecycleManager {6 private val listeners = mutableListOf<LifecycleListener>()7 fun addListener(listener: LifecycleListener) {8 if (listeners.contains(listener)) {9 throw IllegalArgumentException("You can only register a listener once.")10 }11 listeners.add(0, listener)12 }13 fun beforeExecuteTest(test: TestScope) {14 listeners.forEach { it.beforeExecuteTest(test) }15 }16 fun afterExecuteTest(test: TestScope) {17 listeners.reversed().forEach { it.afterExecuteTest(test) }18 }19 fun beforeExecuteGroup(group: GroupScope) {20 listeners.forEach { it.beforeExecuteGroup(group) }21 }...
LifecycleListener
Using AI Code Generation
1val listener = object : LifecycleListener {2override fun beforeExecuteTest(test: TestScope) {3println("Test ${test.path} is about to execute")4}5override fun afterExecuteTest(test: TestScope, result: TestResult) {6println("Test ${test.path} has executed with result ${result.status}")7}8}9listeners(listener)10test("first test") {11println("first test")12}13test("second test") {14println("second test")15}16}17object MyTest : Spek({18val listener = object : LifecycleListener {19override fun beforeExecuteGroup(group: GroupScope) {20println("Test suite is about to execute")21}22override fun afterExecuteGroup(group: GroupScope, result: TestResult) {23println("Test suite has executed with result ${result.status}")24}25}26beforeGroup(listener)27afterGroup(listener)28test("first test") {29println("first test")30}31test("second test") {32println("second test")33}34})
LifecycleListener
Using AI Code Generation
1import org.spekframework.spek2.lifecycle.*2class MyTest : Spek({3 val listener = object : LifecycleListener {4 override fun afterExecuteTest(testCase: TestCase) {5 println("After execute test: ${testCase.path}")6 }7 override fun afterExecuteGroup(group: TestGroup) {8 println("After execute group: ${group.path}")9 }10 override fun beforeExecuteTest(testCase: TestCase) {11 println("Before execute test: ${testCase.path}")12 }13 override fun beforeExecuteGroup(group: TestGroup) {14 println("Before execute group: ${group.path}")15 }16 }17 beforeEachTest {18 println("Before each test")19 }20 afterEachTest {21 println("After each test")22 }23 beforeGroup {24 println("Before group")25 }26 afterGroup {27 println("After group")28 }29 beforeEachGroup {30 println("Before each group")31 }32 afterEachGroup {33 println("After each group")34 }35 beforeEachTest(listener)36 afterEachTest(listener)37 beforeGroup(listener)38 afterGroup(listener)39 beforeEachGroup(listener)40 afterEachGroup(listener)41 group("group 1") {42 test("test 1") {43 println("Test 1")44 }45 }46 group("group 2") {47 test("test 2") {48 println("Test 2")49 }50 }51})
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!!