Mockito is a java-based mocking framework that works with other testing frameworks like JUnit and TestNG. It uses the Java Reflection API internally and allows you to create service objects. Mock objects return dummy data while reducing external dependencies. It simplifies test development by mocking external dependencies and incorporates the mocks into the code under test.

If you are preparing for a Mockito interview in the future and are looking for a quick guide before your interview, then you have come to the right place.
This article will discuss the top 30 mockito interview questions, which consist of all types of mockito interview questions.
Now, let us briefly describe some helpful mockito interview questions.
Easy level questions
1. Define Spy in unit testing?
Ans: A spy is a form of partial mock that Mockito supports. We will take an existing object and "replace" only a portion of its methods while spying. It's useful when we have a large class and only want to duplicate a few methods (partial mocking).
2. What are the limitations of Mockito?
Ans: Some drawbacks of Mockito are:
- It can not mock static methods.
-
Classes, constructors and private methods can not be mocked.
3. When is Mocking Essential?
Ans: It is necessary when:
🔺The component under test has dependencies that have not yet been implemented or are in the process of being implemented.
🔺A brilliant example is a REST API endpoint which will be available later, but you have used it in the code via a dependency.
🔺Because the actual implementation is still not public, you should know what can be the expected answer of that API. Mocks enable you to test the type of integration.
🔺The state of the system is updated by the component.
4. Define unit testing?
Ans: Unit testing confirms that individual units of code (usually functions) work independently as it is expected. Generally, you develop your own test cases to cover the code you wrote. Unit tests confirm that the component you built works perfectly when it operates independently.
Unit test is the piece of code developed by a developer that executes a specified functionality in the code under test.
5. Define Hamcrest?
Ans: Hamcrest is a well-known framework that allows us to design matcher objects. It is used to write software tests and unit tests in the Java programming language. Hamcrest is primarily used in combination with other unit testing frameworks like JUnit, jMockit, and Mockito. It allows for the creation of customized assertion matches, which allow match rules to be specified declaratively. Unit testing frameworks use these matches.
6. What are the benefits of Mockito?
Ans: Mockito has the following advantages:
🔺It can accept return values.
🔺It allows for exceptions.
🔺It supports the generation of mocks via annotation.
🔺Mockito eliminates the requirement for you to create mock objects on your own.
7. Can You Describe the Mockito Framework?
Ans: In Mockito, you should always check a specific class. The mock object is used to introduce the dependency in that class. So, if you have a service class, the Dao class is provided as a mockDao. This enables us to look at the method of that particular service class and determine whether or not it is performing as planned.
Assume the service class contains an updateObject Method. The method of updating is determined by Dao1 and Dao2. The goal here is to test only the updateObject function and not the Dao1 and Dao2 methods. As a result, we can construct a mockDao and return a mock object. This object is not in the database and was created from scratch. It simply checks to see if the updates are going on as planned. The below is the syntax for creating a Mock object:
private static codingninjas mockLedgerBook; //you create the mock object in the setup
setup()
{
mockLedgerBook = Mockito.mock(codingninjas.class);
}
//you call this mockLedgerBook as follows:
LedgerBook ledgerBook = new LedgerBook();
ledgerBook.setName("Coding ninjas");
Mockito.when(mockLedgerBook.findBookById(Mockito.anyLong()).
thenReturn(ledgerBook);
8. What will you do if you want to ensure that a specific class method is called?
Ans: If it's a class's static method, we can use the verify to ensure it's being called.
If the method is an instance method, we can mock the object and then use verify with the mocked object to ensure that the function is called.
9. Can Mockito be used to mock private methods?
Ans: Mocking private or static methods is not supported by Mockito. To test private methods, we need to edit the code to change the access to protected (or package), and static/final methods should be avoided. Certain frameworks, such as PowerMock, do, provide mimicking for private and static functions.
10. Define Mockito inline?
Ans: From version 2.7.6, Mockito offers the 'mockito-inline' component, which enables inline mock creation without supplying the MockMaker extension file. Mockito supports complex mocking features as an option, such as copying final classes. To make this work, use a separate mechanism ("mock maker") that is considered experimental and hence turned off by default.
Intermediate level questions
11. Why Mockito.any is used?
Ans: Mockito.any(Class), Mockito.anyString, Mockito.anyLong, and other methods can be used to test that a method is being called with any argument instead of a specified parameter.
12. Justify "Mockito thread is safe".
Ans: Mockito works well with threads in healthy situations. You can run the tests in parallel to speed up the build. To test in parallel situations, you can also allow several threads to call methods on a shared mock. Consider using the timeout() function to test concurrency.
Mockito, on the other hand, is only thread-safe in healthy tests, tests in which no multiple threads are stubbing/verifying a shared mock. Stubbing or verifying a shared mock from several threads is NOT the right technique to test because it always results in irregular behavior. Mutable state + assertions in a multi-threaded context provide random results. If you stub/verify a shared mock across threads, you can encounter exceptions such as: WrongTypeOfReturnValue, etc.
13. Can I validate Tostring()?
Ans: No. You can, though, stub it. ToString() verification is not implemented because:
When debugging, the IDE uses the function toString() on objects to print local variables and their contents, among other things. The verification of function toString() will most likely fail after debugging.
function toString() is the function that is used for logging or string concatenation. These invocations are normally insignificant, but they do affect the outcome of verification.
14. Define EasyMock.
Ans: EasyMock is a framework for constructing mock objects for a specified interface by using Java reflection. It eliminates the need of the user to hand-write mock objects by employing a dynamic mock object generator.
15. Differentiate between Assert and Verify.
Ans: Assert: If the assert condition is true, the program flow will execute the next test step; else the execution will stop and no further test steps will be executed.
Verify: If the verify condition is true or incorrect, the test execution won't stop.
16. How can I use Mockito to mock void methods?
Ans: The collaborator in test cases will be the MyList class shown below.
List is a public class that extends AbstractList.
public class codingninjas extends AbstractList {
@Override
public void mockito(int index, String element) {
// code
}
}
17. What is the difference between @Mock and @InjectMocks?
Ans: The Mock annotation generates mocks.
The InjectMocks function generates class objects. When the actual method body for a specific class needs to be run then use @InjectMocks.
18. Define Junits.
Ans: JUnit is a framework for unit testing in the Java programming language. JUnit has played an essential role in the evolution of test-driven development. JUnit is part of the xUnit family of unit testing frameworks, which started with SUnit.
At compilation time, JUnit is connected as a JAR.
JUnit created the concept of first testing and then coding to ensure that test data is set up and the expected output is defined before coding. This technique boosts program code productivity and stability while decreasing debugging time.
19. Define Spying in Mockito.
Ans: Mockito allows you to develop spy on real-world things. When a spy is performed, the actual method of the real object is invoked.
syntax:
//create an actual object on spy
calcService = spy(calculator);
//perform operation on the real object
Assert.assertEquals(mathApplication.add(20.0, 5.0),30.0,0);
20. Define ArgumentCaptop in Mockito.
Ans: ArgumentCaptor is a class which is used to save the values of arguments for future assertions. This class is defined in the package org.mockito and can be imported from there.
Some of the methods in this class are as follows:
capture(), getValue(), getAllValues(), and ArgumentCaptor <U> forClass are all functions.