Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 5
    Thanks for this writeup. A NullPointerException with the when/thenReturn format was causing me problems, until I changed it to doReturn/when. Commented May 10, 2015 at 3:05
  • this helped me in particular: "Matcher methods can't be used as return values; there is no way to phrase thenReturn(anyInt()) or thenReturn(any(Foo.class))" Commented Aug 8, 2023 at 23:40
  • Jeff, this is a fantastic answer, and for me, it's worth returning back to over and over, even though I have a pretty good handle on the topic. I upvoted this answer years ago. But re-reading it, I think there might be a very small error in it. In the second bullet point near the top, you mention that Mockito 2.0+ drops the dependency on Hamcrest. I'm reasonably sure that this change actually happened with Mockito 2.1.0, not 2.0.0. Maybe you'd consider checking this and editing it. Commented Sep 7, 2023 at 7:53
  • @DawoodibnKareem Thank you for the compliment! This one is tricky: Mockito 2.0 went through 111 release candidates but never saw a non-beta release, so publicly 1.10.19 and 2.1.0 are adjacent. That said, the change to a provided dependency is a part of the upgrade to Hamcrest 1.3 in 2.0.26-beta documented in 2.0.24-beta as a 2.0 feature. It's still "2.0+" ("2.0.26-beta+"). Commented Sep 7, 2023 at 15:52
  • Oh, wow, OK. I never realised that 2.0 didn't get out of beta. Please accept my apologies for my incorrect "correction". Commented Sep 9, 2023 at 3:29