How do you verify that a method was called a specific number of times in Mockito?

How do you verify that a method was called a specific number of times in Mockito?

A) By using verify(mock, times())
B) By using assert(mock, times())
C) By using verify(mock, count())
D) By using assert(mock, count())

Answer:

A) By using verify(mock, times())

Explanation:

In Mockito, you can verify that a method was called a specific number of times by using the verify(mock, times()) method. This is useful for ensuring that a method is invoked the correct number of times during the test, particularly in cases where methods are expected to be called multiple times.

For example:


verify(mockObject, times(2)).someMethod();

In this example, the verify() method checks that someMethod() was called exactly twice on mockObject. If the method was called more or fewer times, the test will fail.

Reference links:

https://www.javaguides.net/p/mockito-tutorial.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top