One more threat of Mockito

"a thing" should {
"do something" in new Scope {
// setup data
// setup mocks (returns/throws)
thing.doSomething(input) must beCorrect
got {
// check calls to mocks
noMoreCallsTo(mocks)
}
}
}
"manager" should {
"change and store state" in new Scope {
val data = new MutableContainer(...) // original state
val dao = mock[Dao]
val manager = new Manager(dao)
manager.changeAndStore(data)
got {
dao.update(beMutableContainer(...)) // final expected state
noMoreCallsTo(dao)
}
}
}
case class MutableContainer(var list: Seq[String])

object MutableMutator {
def mutate(c: MutableContainer)(pf: PartialFunction[String, String])= {
c.list = c.list.map(s => if (pf.isDefinedAt(s)) pf(s) else s)
}
}
MutableMutator.mutate(c) {
case s@"a" =>
val newValue = s + "b"
dao.update(c)
newValue
}

Ok, what to do?

Besides avoiding of mutable data structures...

val dao = mock[ContainerDao]
.defaultAnswer(i => throw new Ex(s"Unexpected call: $i"))
doAnswer(_ => {}).when(dao).update(...) // final expected state

Conclusion

I don’t know enough about JMock to make a decision to switch from Mockito to JMock. But at least, it’s worth to think about use throwing default answer with Mockito. (and don’t use mutable data structures :) )

--

--

Software developer, moved to Israel from Russia, trying to be aware of things.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dmitry Komanov

Dmitry Komanov

Software developer, moved to Israel from Russia, trying to be aware of things.