Table of contents
1.
Introduction
2.
Inorder Object
2.1.
verify(method)
2.2.
verify(method, mode)
2.3.
verifyNoMoreInteractions()
3.
Verification
3.1.
Syntax
3.2.
Example
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Ordered Verification

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

When writing methods that are to be executed in a particular order, it is always better to know how to check the order of the methods called using Mockito tests. This is where the Inorder object and Verify method to be specific comes into play and makes our life significantly easier. 

Inorder Object

Inorder object is an interface that has three methods that help in checking the order in which the methods are called. 

verify(method)

This takes one parameter, and checks whether the method is called once in order or not

verify(method, mode)

When taking in two parameters it just checks whether the method was called in order or not.

verifyNoMoreInteractions()

This method ensures that no more interactions happened in order in the test.

Now that we know about the different methods we can learn how to use them. In this blog, we will specifically learn about the verify method only.

Verification

Here we will be verifying using the Inorder object and verify method.

Syntax

//create an inOrder verifier for the mocks
InOrder inOrder = Mockito.inOrder(mock1, mock2);

//following will make sure that add is first called then subtract is called.
inOrder.verify(mock1).firstMethod();
inOrder.verify(mock2).secondMethod(Params1, Params2);
You can also try this code with Online Java Compiler
Run Code

Example

First we create an interface that contains all the methods that we want to check the order for.

public interface CalculatorService {
  public double add(double input1, double input2);
  public double subtract(double input1, double input2);
  public double multiply(double input1, double input2);
  public double divide(double input1, double input2);
}
You can also try this code with Online Java Compiler
Run Code

Then we just mock the methods we need to test in the subsequent tests.

public void pretest(){
  calcService = mock(CalculatorService.class);
}
public void mock(){
  //first we add a behaviour to mock the methods
  when(calcService.add(20.0,10.0)).thenReturn(30.0);
  when(calcService.subtract(20.0,10.0)).thenReturn(10.0);

  //then we test the functionalities 
  Assert.assertEquals(calcService.add(20.0,10.0),30.0);
  Assert.assertEquals(calcService.subtract(20.0,10.0),10.0);

  //create a verifier using Inorder for the mocks
  Inorder inOrder = inOrder(calcService);

  //this ensures a verification of the order
  inOrder.verify(calcService).subtract(20.0,10.0);
  inOrder.verify(calcService).add(20.0,10.0);
}
You can also try this code with Online Java Compiler
Run Code

FAQs

1. Difference between verifyNoMoreInteraction and verifyNoInteractions?

Ans: verifyNoMoreInteractions checks that no interactions is left for verification while verifyNoInteractions checks that no interactions happened on given mocks.
 

2. Can we use Jest for verifying in React?

Ans: Yes, Jest can be used for React along with many other frameworks.

 

3. Can I mock private methods?

Ans: When private methods are asserted from a standpoint of testing, these methods do not exist as they are not accessible to the test suite.

 

Key Takeaways

In this blog, we discussed how we can verify the order of the method calls using Inorder Object and verify method.

You may want to learn more about other mocking and it’s need here.

Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!

Happy Learning!

Live masterclass