Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to verify a method called when one of the method parameters are generated in the object under test in mockito?

I’m calling a method on a mock in a service I am testing.

The mock is a class for updating database data, I want to verify that the update method is called.
The code looks like this:

verify(updateLockedAccountStatusMock, times(1)).update(1, 3, true, new Date());

I know this can’t work, since the Date object that is passed into the mock in the class under test is generated in the class just before the method is called. I also know that I can’t call any() here unless I call any() for every parameter. I’d rather not do that as the same method is called elsewhere in the code.
Is there a way to handle cases like this? Or is it simply a matter of passing in any() for every parameter and verifying the amount of times it’s called?
In particular I want to see that the third parameter true is correct, but the date is the only one I don’t really care about at the moment.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use any() for one argument and other matchers (such as eq) for the other arguments.

verify(updateLockedAccountStatusMock, times(1)).update(eq(1), eq(3), eq(true), any());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading