Disclosure: This article may contain affiliate links. When you purchase, we may earn a small commission.

Top 20 Mockito Interview Questions with Answers for Java Programmers

Hello guys, if you are preparing for Java developer interview then you must prepare for Mockito, one of the most popular mocking and unit testing library for Java developers. Mockito is used across all kind of companies, both big and small like startups for unit testing Java applications and that's why good knowledge of Mockito is required to crack the Java Interview. Along with JUnit, Mockito is quite important and if you are looking for Mockito Interview questions and answers then you have come to the right place. Earlier, I have shared best JUnit and Mockito courses and unit testing books, and even popular JUnit interview questions and in this article, I am going to share 20 common Mockito questions for your interviews.


20 Mockito Interview Questions with Answers 

Are you in dire need of passing your Mockito interview which is just around the corner? Worry no more because you are about to achieve that. Am sure you have been looking for Mockito interview questions so as to have a clue of what usually transpires in an interview of that kind. 

This is the right article for you because the top 20 Mockito interview questions with answers have been listed down below. Please go through them and you will be sure of passing your interview.


1. What is Mockito?
Answer: Mockito is an open-source testing framework for Java released under the MIT License. This framework allows the creation of test double objects in automated unit tests for the purpose of test-driven development or behavior-driven development. Mockito allows developers to verify the behavior of the system under test (SUT) without establishing expectations beforehand.

Top 20 Mockito Interview Questions With Answers
 


2. When is Mocking required?
Answer: it is required when:

  • The component under test has dependencies that are not yet implemented or the implementation is in progress.
  • A good example can be a REST API endpoint which will be available later at some point in time, but you have consumed it in the code via a dependency.
  • Now as the real implementation is still not available, you really know most of the time what is the expected response of that API. Mocks allow you to test those kinds of integration.
  • Component updates the state in the system.


3. What are the steps to be performed while using the Junit with Mocking framework?
Answer:

  • Initialize required objects for working with mocks and tested method
  • Set the mock behavior on dependent objects
  • Execute the tested method
  • Perform assertions
  • Verify if a method is invoked or not


4. List some Mockito Annotations?
Answer:

  • @Mock - It is used to create and inject mocked instances.
  • @Spy - It is used to create a real object and spy on the real object.
  • @Captor - It is used to create an ArgumentCaptor.
  • @InjectMocks - It is used to create an object of a class and insert its dependencies.
  • @RunWith - It is utilized to keep the test clean and improves debugging. It additionally detects the unutilized stubs available in the test and initializes mocks annotated with @Mock annotation.


5. What is mocking in testing?
Answer: In testing, Mocking is a process that is used to isolate and focus on the code that is being tested rather than the behavior or state of external dependencies.


6. What are some benefits of Mockito?
Answer: Major Benefits for using Mockito for testing are:

  • No need to write your mock objects by hand.
  • Safe refactoring and supports exception handling
  • Supports Annotation
  • Order check and Return value support


7. What are the limitations of Mockito?
Answer: Some of the limitations of Mockito are as follows:

  • It cannot mock constructors or static methods.
  • It requires Java version 6 plus to run.
  • It also cannot mock equals (), hashCode() methods.
  • VM mocking is only possible on VMs that are supported by Objenesis.


8. What is EasyMock?
Answer: EasyMock is a framework for creating mock objects as it uses Java reflection to create it for a given interface. It relieves the user of hand-writing mock objects as it uses a dynamic mock object generator.


9. What is the difference between Assert and Verify?
Answer:

  • Assert: If the assert condition is true then the program control will execute the next test step but if the condition is false, the execution will stop and further test steps will not be executed.
  • Verify: There won't be any halt in the test execution even though the verify condition is true or false.


10. What is Hamcrest used for?
Answer:
Hamcrest is a assert framework for testing libraries like JUnit. Hamcrest allows checking for conditions in our code via subsisting matcher classes. It additionally sanctions us to define our custom matcher implementations.


11. What is the use of @Spy annotation?
Answer: The @Spy annotation is used to create a real object and spy on that real object. A spy helps to call all the normal methods of the object while still tracking every interaction, just as we would with a mock.


12. What is the use of @Captor annotation?
Answer: We can use @Captor annotation to create an argument captor at the field level.


13. What is the difference between @InjectMocks and @mock?
Answer:

  • @Mock creates a mock implementation for the classes you need.
  • @InjectMock creates an instance of the class and injects the mocks into it.


14. What Is The Use Of Mockito.any?
Answer: Mockito.any(Class) can be used in case we need to verify that a method is being called with any argument and not a specific argument.


15. How Should We Ignore Or Avoid Executing Set Of Tests?
Answer: We can remove @Test from the respective test so as to avoid its execution. Alternatively, we can put the @Ignore annotation on the Junit file if we want to ignore all tests in a particular file.


16. Do You Mock Classes & Interfaces?
Answer: Yes, the API is the same for mocking classes or interfaces.


17. Can I Mock Static Methods?
Answer: No. Mockito prefers object orientation and dependency injection over static, procedural code that is hard to understand and change.


18. Name Few Java Mocking Frameworks
Answer: Mockito, PowerMock, EasyMock, JMock, JMockit


19. Can you mock private methods using mockito?
Answer: No, Mockito does not allow us to mock private methods.


20. What is the use of the mock() method?
Answer: The Mock() method is used to create and inject the mocked instances. The other way of creating the instances is using the @mock annotations.



Lastly, all you should know and understand is that the questions and answers mentioned above are what will propel you to the desired success when interview day comes. They are just simple questions like any other you can meet anywhere. Don’t be afraid because if you have keenly gone through them, you will be able to answer them with ease and the interview panel will be impressed with you. Wish you the best of luck.

No comments :

Post a Comment