Mockito Ordered Verification - Mockito

How to make an order verification with Mockito?

Here, Mockito provides Inorder class which takes care of the order of method calls that the mock is going to make in due course of its action.

Syntax

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

Here,testing is performed by injecting in it a mock of calculatorService. Mock will be created by Mockito.
Here we've added two mock method calls, add() and subtract(), to the mock object via when(). However during testing, we've called subtract() before calling add(). When we create a mock object using Mockito, the order of execution of the method does not matter. Using InOrder class, we can ensure call order.

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