Created
October 14, 2020 22:47
-
-
Save AfzalivE/c4201529a7fe4e8e937d3f6c690e208d to your computer and use it in GitHub Desktop.
fake() for all the unit tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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