Sometimes, when I mock a method in Mockito, I used anyString instead of any and I get into a mess and my test fails. Why?
given(mUserRepo.getUser(anyString(). any())).willReturn(new User());
And what will happen if
UserRepo is called with null? Well, the matcher doesn’t work and the test fails. anyString() does not work with null values. If you pass null to a mocked service then use any() as a matcher.
Probably, most of you already know this, but it is good to keep it in mind when you want to dynamically change the values returned by a mock you have.
Continue reading “How to mock a dynamic value returned from a method with Mockito?”
According to this issue, Mockito cannot capture var-args still.
There is a very easy way to verify a method has been called with the right parameter in Mockito. All you need to do is use captors, to capture the actual argument. Here is how you can use them:
ArgumentCaptor argument = ArgumentCaptor.forClass(RuleEntity.class);
Simple it is, just a quick reminder for starters.