Table of contents
1.
Introduction
2.
Expecting Calls
2.1.
Example
2.1.1.
Program
2.1.2.
Program
3.
Frequently Asked Questions
4.
Conclusions
Last Updated: Mar 27, 2024
Easy

Expecting Calls - Mockito

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

Introduction

When mocking specific methods using Mockito sometimes, we need to check the number of calls made on a particular method. These help us in making sure that the methods in use are restricted with the number of times they are called.

In this blog, we will learn about the expecting calls that help us in mitigating the above problem and thus add the functionality of maintaining the number of calls made using Mockito for mocking.

Expecting Calls

When we use mockito, it provides us with a special check on the number of calls that are made by a particular method. For example we have an interface that we must make sure is called only once, we can use these methods to check the same.

Example

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

Program

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 need to create a java class that can represent the MathApplication to contain our methods.

Program

public class MathApplication{
	private CalculatorService;
	public void setCalculatorService(CalculatorService calcService){
		this.calcService = calcService;
	}
}


public double add(double input1, double input2){
	return calcService.add(input1,input2);
}
You can also try this code with Online Java Compiler
Run Code

Next we need to mock the behaviour of the above method so as to create our test even though the actual functionality is not ready.

when(calcService.add(10.0, 20.0).thenReturn(30.0));
You can also try this code with Online Java Compiler
Run Code

Now we can check whether the add method is called a particular number of times using the below example.

verify(calcService.times(2)).add(10.0,20.0); //simply executing the tests gives us the result as True.
You can also try this code with Online Java Compiler
Run Code

Frequently Asked Questions

1. Can we use .times(0)?

Ans: Rather than using .times(0) it would be better to use .never().
 

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.

 

4. What are the reasons to use mock objects in unit tests?
Ans: In a unit test, mock objects can assume the behavior of complex and real objects. They are extremely helpful when a real object is impractical or impossible to incorporate into a unit test.
 

Conclusions

In this blog, we discussed how we can use Expecting calls in Mockito.

You may want to learn more about Mockito Annotations hereWe hope that this blog has helped you enhance your knowledge regarding Expecting Calls in Mockito. Do upvote our blog to help other ninjas grow.

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