CoroutineTest

@ExperimentalCoroutinesApi
@ExtendWith(value = [CoroutineTestExtension::class])
annotation class CoroutineTest(scopeFactory: KClass<*>)

Annotation for specifying a custom CoroutineTestExtension.ScopeFactory while extending a test class or function with CoroutineTestExtension.

Samples

import dispatch.test.CoroutineTest
import dispatch.test.TestProvidedCoroutineScope
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
fun main() { 
   //sampleStart 
   @CoroutineTest
class CoroutineTestDefaultFactorySample(
  val testScope: TestProvidedCoroutineScope
) {

  @Test
  fun `extension should automatically inject into test class`() = runBlocking {

    val subject = SomeClass(testScope)

    val resultDeferred = subject.someFunction()

    testScope.advanceUntilIdle()

    resultDeferred.await() shouldBe someValue
  }
} 
   //sampleEnd
}
import dispatch.test.CoroutineTest
import dispatch.test.CoroutineTestExtension
import dispatch.test.TestProvidedCoroutineScope
import io.kotest.matchers.shouldNotBe
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
fun main() { 
   //sampleStart 
   class CoroutineTestNamedFactorySample {

  class TestCoroutineScopeWithJobFactory : CoroutineTestExtension.ScopeFactory() {

    override fun create(): TestProvidedCoroutineScope {
      return TestProvidedCoroutineScope(context = Job())
    }
  }

  @CoroutineTest(TestCoroutineScopeWithJobFactory::class)
  class CustomFactorySample(val testScope: TestProvidedCoroutineScope) {

    @Test
    fun `injected scope should have a Job context`() = runBlocking {

      testScope.coroutineContext[Job] shouldNotBe null
    }
  }
} 
   //sampleEnd
}

See also

Constructors

Link copied to clipboard
fun CoroutineTest(scopeFactory: KClass<*> = CoroutineTestExtension.ScopeFactory::class)

Properties

Link copied to clipboard
val scopeFactory: KClass<*>

optional KClass which extends CoroutineTestExtension.ScopeFactory. This class must have a default constructor An instance will be automatically initialized inside the CoroutineTestExtension and used to create custom TestProvidedCoroutineScope instances.

Sources

Link copied to clipboard