I'd like to stub some code so that a vararg method returns true when one of the arguments matches a specific value. For example, given the existing code that I can't change:
(Using Kotlin here, but I figure this applies to any Java situation.)
class Foo {
fun bar(vararg strings : String) : Boolean {
// Iterates `strings` and returns true when one satisfies some criteria
}
}
... I want to write stub code similar to this:
val foo = Foo()
whenever(foo.bar(eq("AAA"))).thenReturn(true)
This works fine when the call is exactly foo.bar("AAA")
.
However, there are times when the code under test makes the call foo.bar("AAA", "BBB")
, and in those cases, it fails.
How can I revise my stub code so it works when any number of varargs are passed in the call?
Edit Flagged as a possible duplicate; in that case, the scenario contemplates the complete omission of the varargs in the call. Here, I'm trying to match one specific element of the varargs array.