TestMosaicBuilder

Fluent configuration for scoped TestMosaic execution with substituted dependencies.

This class provides a fluent API for setting up test scenarios by configuring mock tiles with various behaviors. It supports both Tile and MultiTile mocks with different behaviors like success, failure, delays, and custom logic. Each withMosaic invocation inherits its calling coroutine context, including the Job, dispatcher, and scheduler when called inside runTest.

Basic Usage

mosaicBuilder()
.withMockTile(MyTile, "test data")
.withFailedTile(OtherTile, RuntimeException("Test error"))
.withDelayedTile(SlowTile, "delayed data", 1000) // 1 second delay
.withMosaic { /* compose and assert here */}

MultiTile Usage

mosaicBuilder()
.withMockTile(UserTile, mapOf("user1" to user1, "user2" to user2))
.withCustomTile(ProfileTile) { keys ->
// Custom logic based on requested keys
keys.associateWith { key -> createMockProfile(key) }
}
.withMosaic { /* compose and assert here */}

Constructors

Link copied to clipboard
constructor()

Functions

Link copied to clipboard
inline fun <T : Any> withCanvasSource(obj: T): TestMosaicBuilder

Borrows a caller-owned source for the canvas with the specified object.

inline fun <T : Any> withCanvasSource(qualifier: String, obj: T): TestMosaicBuilder

Borrows a caller-owned source for the canvas with the specified object and qualifier.

fun <T : Any, V : T> withCanvasSource(clazz: KClass<T>, obj: V): TestMosaicBuilder

Borrows a caller-owned source for the canvas with the specified class and object. Allows for passing in a superclass as the retrieval type.

Borrows a caller-owned source for the canvas using a CanvasKey.

fun <T : Any, V : T> withCanvasSource(clazz: KClass<T>, qualifier: String, obj: V): TestMosaicBuilder

Borrows a caller-owned source for the canvas with the specified class, qualifier, and object.

Link copied to clipboard
@JvmName(name = "withCustomMultiTile")
fun <K : Any, V> withCustomTile(tile: MultiTile<K, V>, provider: suspend Mosaic.(Set<K>) -> Map<K, V>): TestMosaicBuilder

Adds a mock MultiTile with custom behavior.

fun <V> withCustomTile(tile: Tile<V>, provider: suspend Mosaic.() -> V): TestMosaicBuilder

Adds a mock Tile with custom behavior.

Link copied to clipboard
@JvmName(name = "withDelayedMultiTile")
fun <K : Any, V> withDelayedTile(tile: MultiTile<K, V>, response: Map<K, V>, delayMs: Long): TestMosaicBuilder

Adds a mock MultiTile that delays before returning responses.

fun <V> withDelayedTile(tile: Tile<V>, response: V, delayMs: Long): TestMosaicBuilder

Adds a mock Tile that delays before returning the response.

Link copied to clipboard
@JvmName(name = "withFailedMultiTile")
fun withFailedTile(tile: MultiTile<*, *>, throwable: Throwable): TestMosaicBuilder

Adds a mock MultiTile that fails with the specified exception.

fun withFailedTile(tile: Tile<*>, throwable: Throwable): TestMosaicBuilder

Adds a mock Tile that fails with the specified exception.

Link copied to clipboard
@JvmName(name = "withMockMultiTile")
fun <K : Any, V> withMockTile(tile: MultiTile<K, V>, response: Map<K, V>): TestMosaicBuilder

Adds a mock MultiTile that returns the specified responses for given keys.

fun <V> withMockTile(tile: Tile<V>, response: V): TestMosaicBuilder

Adds a mock Tile that returns the specified response.

Link copied to clipboard
suspend fun <R> withMosaic(block: suspend TestMosaic.() -> R): R

Executes block with a fresh cache and a snapshot of this builder's configuration. Inherits the calling coroutine's Job, dispatcher, scheduler, and other context elements. Every exit cancels unfinished producers and waits for their attached children to clean up. Supplied Canvas sources are borrowed: they remain owned by the caller and are never closed. Configure this builder from one coroutine at a time; later changes affect only later executions.