Merge "Add mockito helpers" into sc-v2-dev am: 9d9ea2c0ff

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/16140983

Change-Id: Idc40a2feab87294478969976a448a38008317f08
This commit is contained in:
TreeHugger Robot
2021-10-29 18:19:18 +00:00
committed by Automerger Merge Worker

View File

@@ -58,3 +58,24 @@ fun <T> capture(argumentCaptor: ArgumentCaptor<T>): T = argumentCaptor.capture()
*/
inline fun <reified T : Any> argumentCaptor(): ArgumentCaptor<T> =
ArgumentCaptor.forClass(T::class.java)
/**
* Helper function for creating new mocks, without the need to pass in a [Class] instance.
*
* Generic T is nullable because implicitly bounded by Any?.
*/
inline fun <reified T : Any> mock(): T = Mockito.mock(T::class.java)
/**
* Helper function for creating and using a single-use ArgumentCaptor in kotlin.
*
* val captor = argumentCaptor<Foo>()
* verify(...).someMethod(captor.capture())
* val captured = captor.value
*
* becomes:
*
* val captured = withArgCaptor<Foo> { verify(...).someMethod(capture()) }
*/
inline fun <reified T : Any> withArgCaptor(block: ArgumentCaptor<T>.() -> Unit): T =
argumentCaptor<T>().apply { block() }.value