Skip to content

Instantly share code, notes, and snippets.

@mg6maciej
Last active April 3, 2017 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mg6maciej/df93c5a46a218cc0ab0e0a20cc79bc1b to your computer and use it in GitHub Desktop.
Save mg6maciej/df93c5a46a218cc0ab0e0a20cc79bc1b to your computer and use it in GitHub Desktop.
fun <T> stubbable(function: KCallable<T>) = object : ReadWriteProperty<Any?, T> {
override fun getValue(thisRef: Any?, property: KProperty<*>) = throw UnsupportedOperationException()
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
val args = listOf(thisRef) + function.parameters.drop(1).map { anyList<Any>() }
whenever(function.call(*args.toTypedArray())).thenReturn(value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment