Skip to content

Instantly share code, notes, and snippets.

@AfzalivE
Created Oct 14, 2020
Embed
What would you like to do?
fake() for all the unit tests
val ALL_FAKES = listOf(
FakePostLoginUseCase::class,
// all fake classes go here
)
/**
* Finds and initializes an instance of
* class [T] if it exists in [ALL_FAKES].
*
* If it doesn't, a [FakeNotFoundException] is thrown
*/
inline fun <reified T : Any> fake(): T {
val match = ALL_FAKES.firstOrNull {
it.isSubclassOf(T::class)
}
match ?: throw fakeNotFoundException<T>()
return match.createInstance() as T
}
inline fun <reified T : Any> fakeNotFoundException() = FakeNotFoundException(T::class.simpleName)
class FakeNotFoundException(kClass: String?) : Throwable("No matching fakes were found for type: $kClass")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment