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.

File: CalculatorService.java

Example

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

File: CalculatorService.java

Step 2 − Create a JAVA class to represent MathApplication

File: MathApplication.java

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.

File: MathApplicationTester.java

Step 4 − Execute test cases

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

File: TestRunner.java

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 DMCA.com Protection Status

Mockito Topics