10 Mockito Interview Questions and Answers in 2023

Mockito icon
As Mockito continues to be a popular tool for unit testing in Java, it is important to be prepared for any Mockito-related questions that may come up in a job interview. This blog post will provide an overview of 10 Mockito interview questions and answers that are likely to be asked in 2023. We will discuss the questions and provide detailed answers to help you prepare for your upcoming interview.

1. How would you explain the concept of Mockito to a non-technical person?

Mockito is a powerful tool used by software developers to create and manage mock objects. Mock objects are simulated versions of real objects that can be used to test the behavior of a program without having to use the real objects. Mockito allows developers to create and manage these mock objects in a simple and efficient way. It also provides a framework for writing tests that use the mock objects. With Mockito, developers can quickly and easily create tests that simulate the behavior of their code without having to use the real objects. This makes it easier to test the code and ensure that it works as expected.


2. What is the difference between a mock and a stub in Mockito?

Mock and stub are two different types of test doubles used in Mockito. A mock is an object that is used to simulate the behavior of a real object in a controlled environment. It is used to verify the behavior of the system under test. A stub is an object that is used to provide a predetermined response to a method call. It is used to control the flow of the system under test.

Mocks are typically used to verify the behavior of the system under test. They are used to check that the system is behaving as expected. Mocks can be used to verify that a method is called with the correct parameters, that a method returns the expected value, or that a method throws an exception when it should.

Stubs are typically used to control the flow of the system under test. They are used to provide a predetermined response to a method call. This allows the system under test to be tested in a controlled environment. Stubs can be used to return a predetermined value, throw an exception, or delay the response of a method call.

In summary, mocks are used to verify the behavior of the system under test, while stubs are used to control the flow of the system under test.


3. How do you create a mock object in Mockito?

Creating a mock object in Mockito is a simple process. First, you need to create an interface or class to be mocked. This can be done by creating a new class or interface and annotating it with the @Mock annotation.

Next, you need to create an instance of the mock object. This can be done by using the Mockito.mock() method. This method takes the class or interface to be mocked as an argument and returns an instance of the mock object.

Finally, you need to configure the mock object. This can be done by using the when() and thenReturn() methods. The when() method takes a method call as an argument and the thenReturn() method takes the value to be returned when the method is called.

Once the mock object is configured, it can be used in unit tests to simulate the behavior of the real object.


4. What is the purpose of the verify() method in Mockito?

The verify() method in Mockito is used to check if a method of a mock object has been called with certain parameters. It is used to ensure that the code under test is calling the mock object correctly. It is also used to check the number of times a method has been called. This is useful for verifying that a method is called the correct number of times, or that it is called with the correct parameters. The verify() method is also used to check that a method is not called at all. This is useful for verifying that a method is not called when it should not be.


5. How do you verify the behavior of a mock object in Mockito?

Verifying the behavior of a mock object in Mockito can be done using the verify() method. This method takes the mock object as an argument and allows you to specify the behavior that you expect from the mock object. For example, if you want to verify that a method on the mock object was called, you can use the verify() method to check that the method was called with the expected arguments.

You can also use the verifyNoMoreInteractions() method to ensure that no other methods were called on the mock object. This is useful for ensuring that the mock object is not being used in unexpected ways.

Finally, you can use the verifyZeroInteractions() method to ensure that no methods were called on the mock object at all. This is useful for ensuring that the mock object is not being used in any way.


6. What is the difference between a mock and a spy in Mockito?

The difference between a mock and a spy in Mockito is that a mock is a complete fake object that is created to replace a real object, while a spy is a partial fake object that wraps a real object.

Mocks are used when you want to completely replace an object with a fake one. This is useful when you want to test a certain behavior without having to worry about the real object's behavior. For example, if you want to test a method that calls a certain method on an object, you can create a mock of that object and have it return a certain value when the method is called.

Spies, on the other hand, are used when you want to partially replace an object with a fake one. This is useful when you want to test a certain behavior while still using the real object's behavior. For example, if you want to test a method that calls a certain method on an object, you can create a spy of that object and have it call the real method while still returning a certain value when the method is called.

In summary, mocks are used to completely replace an object with a fake one, while spies are used to partially replace an object with a fake one.


7. How do you configure Mockito to use a custom argument matcher?

To configure Mockito to use a custom argument matcher, you must first create the argument matcher. This can be done by implementing the ArgumentMatcher interface and overriding the matches() method. The matches() method should return true if the argument matches the criteria specified in the matcher.

Once the argument matcher is created, it can be used in Mockito by using the when() method. The when() method takes an argument matcher as its first parameter. The argument matcher should be passed in as an anonymous inner class.

For example, if you have a custom argument matcher called MyArgumentMatcher, you can use it in Mockito like this:

when(mockObject.someMethod(argThat(new MyArgumentMatcher()))).thenReturn(someValue);

This will configure Mockito to use the custom argument matcher when the someMethod() method is called with an argument that matches the criteria specified in the MyArgumentMatcher class.


8. How do you configure Mockito to use a custom answer?

To configure Mockito to use a custom answer, you need to use the Answer interface. This interface provides a single method, answer(), which takes an invocation object as an argument. The invocation object contains information about the method being called, such as the method name, arguments, and return type.

Using the Answer interface, you can create a custom answer implementation that will be used whenever Mockito is asked to provide an answer. The implementation should override the answer() method and provide the desired behavior. For example, if you want to return a specific value for a given method, you can create an implementation that checks the method name and arguments, and returns the desired value.

Once you have created your custom answer implementation, you can configure Mockito to use it by calling the Mockito.answer() method. This method takes an Answer instance as an argument, so you can pass in your custom implementation.

Mockito will then use your custom answer whenever it is asked to provide an answer.


9. How do you configure Mockito to use a custom mock object factory?

To configure Mockito to use a custom mock object factory, you need to create a custom implementation of the org.mockito.plugins.MockMaker interface. This interface defines the methods that Mockito will use to create mock objects.

Once you have implemented the MockMaker interface, you need to register it with Mockito. This can be done by creating a file named org.mockito.plugins.MockMaker in the src/test/resources/mockito-extensions directory of your project. This file should contain the fully qualified name of your custom MockMaker implementation.

Finally, you need to configure Mockito to use your custom MockMaker. This can be done by setting the mockito.mock-maker system property to the fully qualified name of your custom MockMaker implementation.

Once you have completed these steps, Mockito will use your custom mock object factory whenever it creates mock objects.


10. How do you configure Mockito to use a custom verification mode?

To configure Mockito to use a custom verification mode, you must first create a custom VerificationMode class that implements the VerificationMode interface. This class should contain the logic for the custom verification mode you wish to use.

Once the custom VerificationMode class is created, you can configure Mockito to use it by calling the verify() method with the custom VerificationMode as an argument. For example:

Mockito.verify(mockObject, new MyCustomVerificationMode());

This will configure Mockito to use the custom verification mode you have created.


Looking for a remote job? Search our job board for 70,000+ remote jobs
Search Remote Jobs
Built by Lior Neu-ner. I'd love to hear your feedback — Get in touch via DM or lior@remoterocketship.com