scala - Mock is returning stubbed result for arbitrary param -
with following test have invalid stub setup. mock requestbuilder stubbed param "incorrect", whereas class under test invokes "/socket".
my experience mockito in junit tells me invocation @ runtime result in null. however, i'm seeing false positive. test passes & requestbuilder returning mock request, though invoked "/socket".
why this? has having more 1 expectation? if so, final expectation 1 counts , should fail.
class channelspec extends specification mockito scalacheck arbitraryvalues { def = s2""" channel must send open request /socket provided channel name on instantiation $sendtosocket """ def sendtosocket = prop{(name: string, key: key, secret: secret) => val requestbuilder = mock[(string, seq[map[string, string]]) => req] val request = mock[req] val httprequestor = mock[(req) => future[string]] val result = mock[future[string]] val params = seq(map("key" -> key.value, "timestamp" -> anystring, "token" -> anystring), map("channel" -> name)) requestbuilder("incorrect", params) returns request httprequestor(request) returns result new channel(name, key, secret, requestbuilder = requestbuilder, httprequestor = httprequestor) there one(requestbuilder).apply("incorrect", params) println("expecting " + request) there one(httprequestor).apply(request) }
while don't know scala, know anystring doesn't think does. specifically, all mockito matchers work through side effects, adding related string objects argumentmatcherstorage described toward end of this answer.
consequently, can't extract matchers variables:
// fine, because parameters evaluated in order. takestwoparameters(eq("a"), eq("b")) returns bar // let's change little bit. val secondparam = eq("second") val firstparam = eq("first") // @ point, firstparam == secondparam == null, , hidden argument // matcher stack looks [eq("second"), eq("first")]. means // stubbing apply takestwoparameters("second", "first"), not // takestwoparameters("first", "second")! takestwoparameters(firstparam, secondparam)) returns bar furthermore, can't nest anystring arbitrary types seq or map; mockito's argument matchers designed match entire arguments.
your best bet use argthat params, , create small hamcrest matcher checks argument you're checking formed. give flexibility of checking params expect, while ignoring values don't care about.
Comments
Post a Comment