Skip to content

Instantly share code, notes, and snippets.

@sczerwinski
Created June 15, 2017 19:31
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 sczerwinski/ae287be7af74a0728ff067a41f25839d to your computer and use it in GitHub Desktop.
Save sczerwinski/ae287be7af74a0728ff067a41f25839d to your computer and use it in GitHub Desktop.
Kotlin functions creating custom Hamcrest matchers for Android
import android.view.View
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher
inline fun <reified T : View> withType(): Matcher<View> = object : TypeSafeMatcher<View>() {
override fun describeTo(description: Description?) {
description?.appendText("with type: ${T::class.java.name}")
}
override fun matchesSafely(item: View?): Boolean =
item.let { it is T }
}
@sczerwinski
Copy link
Author

Usage:

onView(withType<Button>())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment