TestingServiceLocator

abstract class TestingServiceLocator<S>(scope: S) : ScopedServiceLocator<S>

A ScopedServiceLocator subclass designed specifically for use in tests.

This locator provides two key features to simplify testing:

  1. Automatic Mocking: If a service is requested that has not been explicitly registered, this locator will automatically create and return a mock instance by calling the abstract createMock function.

  2. Re-registration: Unlike a production locator, this class allows a key to be registered multiple times, overwriting the previous registration. This is useful for setting up different fakes or mocks for different test cases.

Parameters

S

The type of the scope identifier.

scope

The specific scope instance for this locator, which should be a testing-specific scope.

Constructors

Link copied to clipboard
constructor(scope: S)

Properties

Link copied to clipboard
val scope: S

Functions

Link copied to clipboard
abstract fun <T : Any> createMock(clazz: KClass<T>): T

Creates a mock instance for the given service type.

Link copied to clipboard
fun <T : Any, GetParams> get(key: ServiceKey<T, *, GetParams, *>, params: GetParams): T
Link copied to clipboard
fun <T : Any, GetParams> getOrNull(key: ServiceKey<T, *, GetParams, *>, params: GetParams): T?
Link copied to clipboard
inline fun <T : Any> getOrProvide(noinline allowedScopes: (S) -> Boolean, threadSafetyMode: LazyThreadSafetyMode, noinline provider: () -> T): T
fun <T : Any, GetParams, PutParams> getOrProvide(key: ServiceKey<T, *, GetParams, PutParams>, allowedScopes: (S) -> Boolean, putParams: PutParams, getParams: GetParams): T
Link copied to clipboard
override fun <T : Any, PutParams> onInvalidScope(key: ServiceKey<T, *, *, PutParams>, putParams: PutParams): ServiceEntry<T>

Called when getOrProvide is attempted for a specified key in a disallowed scope.

Link copied to clipboard
fun <T : Any, PutParams> put(key: ServiceKey<T, *, *, PutParams>, params: PutParams)