Skip to content

Instantly share code, notes, and snippets.

@pgreze
Last active November 28, 2018 10:05
Show Gist options
  • Save pgreze/9bb4880e82ae754873dab48e7432d896 to your computer and use it in GitHub Desktop.
Save pgreze/9bb4880e82ae754873dab48e7432d896 to your computer and use it in GitHub Desktop.
Replace Mockito Captor by ArgumentMatcher
class CaptorToArgumentMatcher {
@Test
fun captor() {
ArgumentCaptor.forClass(JSONObject::class.java).also { captor ->
verify(tracker).view(eq("screen event"), captor.capture())
assertThat(captor.value.length()).isEqualTo(3)
assertThat(captor.value.get("number")).isEqualTo(12.3)
assertThat(captor.value.get("string")).isEqualTo("value")
assertThat(captor.value.get("empty")).isEqualTo(null)
}
}
@Test
fun matcher() {
verify(tracker).view(eq("screen event"), isJSONObject(mapOf(
"number" to 12.3,
"string" to "value",
"empty" to null
)))
}
}
private fun isJSONObject(expected: Map<String, Any?>): JSONObject = argThat {
val actual: Map<String, Any?> = keys()
.asSequence()
.filterIsInstance<String>() // Fix Kotlin compiler error
.map { it to get(it) }
.toMap()
return@argThat actual == expected
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment