Best Spek code snippet using org.spekframework.spek2.lifecycle.InstanceFactory.create
JvmDiscoveryContextFactory.kt
Source:JvmDiscoveryContextFactory.kt
...10import kotlin.reflect.full.primaryConstructor11import kotlin.streams.toList12object JvmDiscoveryContextFactory {13 private val defaultInstanceFactory = object : InstanceFactory {14 override fun <T : Spek> create(spek: KClass<T>): T {15 return spek.objectInstance ?: spek.constructors.first { it.parameters.isEmpty() }16 .call()17 }18 }19 fun create(testDirs: List<String>): DiscoveryContext {20 val classes = scanClasses(testDirs)21 val builder = DiscoveryContext.builder()22 classes.filter { !it.isAbstract }23 .filter { it.findAnnotation<Ignore>() == null }24 .map { cls ->25 val instanceFactory = instanceFactoryFor(cls)26 cls to { instanceFactory.create(cls) }27 }.forEach { (cls, factory) ->28 builder.addTest(cls, factory)29 }30 return builder.build()31 }32 private fun instanceFactoryFor(spek: KClass<*>): InstanceFactory {33 return spek.annotations.filterIsInstance<CreateWith>()34 .map { it.factory }35 .map { it.objectInstance ?: it.primaryConstructor!!.call() }36 .firstOrNull() ?: defaultInstanceFactory37 }38 private fun scanClasses(testDirs: List<String>): List<KClass<out Spek>> {39 val cg = ClassGraph()40 .enableClassInfo()...
InstanceFactoryTest.kt
Source:InstanceFactoryTest.kt
...5import kotlin.reflect.KClass6import kotlin.test.assertEquals7const val PARAM_VALUE = 18object CustomFactory: InstanceFactory {9 override fun <T : Spek> create(spek: KClass<T>): T {10 return spek.constructors.first().call(PARAM_VALUE)11 }12}13@CreateWith(CustomFactory::class)14class InstanceFactoryTest(val parameter: Int): Spek({15 test("parameter should be passed") {16 assertEquals(parameter, PARAM_VALUE)17 }18})
InjectionFactory.kt
Source:InjectionFactory.kt
...4import org.spekframework.spek2.lifecycle.InstanceFactory5import org.springframework.test.context.TestContextManager6import kotlin.reflect.KClass7class InjectionFactory: InstanceFactory {8 override fun <T : Spek> create(spek: KClass<T>): T {9 val context = TestContextManager(spek.java).testContext.applicationContext10 val objectMapper = context.getBean(ObjectMapper::class.java)11 return spek.constructors.first().call(objectMapper)12 }13}...
CreateWith.kt
Source:CreateWith.kt
1package org.spekframework.spek22import org.spekframework.spek2.lifecycle.InstanceFactory3import kotlin.reflect.KClass4@Target(AnnotationTarget.CLASS)5@Retention(AnnotationRetention.RUNTIME)6annotation class CreateWith(val factory: KClass<out InstanceFactory>)...
InstanceFactory.kt
Source:InstanceFactory.kt
...3import org.spekframework.spek2.meta.Experimental4import kotlin.reflect.KClass5@Experimental6interface InstanceFactory {7 fun <T : Spek> create(spek: KClass<T>): T8}...
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!!