Mockito Expecting Calls - Mockito

How to check expecting calls with Mockito?

Mockito helps to get the information of number of calls that can be made on a particular method. Suppose MathApplication should call the CalculatorService.serviceUsed() method only once, then it should not be able to call CalculatorService.serviceUsed() more than once.

Create CalculatorService interface as follows.



Step 1 − Create an interface called CalculatorService to provide mathematical functions


Step 2 − Create a JAVA class to represent MathApplication


Step 3 − Test the MathApplication class

Now,start testing the MathApplication class, by injecting in it a mock of calculatorService. Mock will be created by Mockito.


Step 4 − Execute test cases

Create a java class file named TestRunner in C:\> Mockito_WORKSPACE to execute Test case(s).


Step 5 − Verify the Result

Compile the classes using javac compiler as follows –
Now run the Test Runner to see the result –
Verify the output.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Mockito Topics