Freshers Registration

Mockito MCQs and Answers With Explanation | Mockito Quiz

Mockito MCQ's

Mockito MCQs and Answers With Explanation – In today’s world, technology is playing a critical role in software development. Testing is an essential part of software development, and mock objects are one of the essential tools used in testing. Now, aspirants who are curious to learn more about Mockito can refer to this article to gather the Mockito MCQ Questions. We assure you that from this Mockito Quiz, you can gain knowledge from basic to in-depth details about Mockito.

Mockito MCQ Questions

Mockito is a popular mocking framework that allows developers to write tests that isolate components from their dependencies by creating mock objects. These Mockito Multiple Choice Questions will test your knowledge of Mockito and its usage in testing Java applications.

Join Telegram Join Telegram
Join Whatsapp Groups Join Whatsapp

Mockito Multiple Choice Questions

Name Mockito
Exam Type MCQ (Multiple Choice Questions)
Category Technical Quiz
Mode of Quiz Online

Top 40 Mockito MCQs | Practice Online Quiz

1. What is Mockito?

a) A mocking framework for Java
b) A testing framework for Python
c) A database management system
d) A web framework for JavaScript

Answer: (a) A mocking framework for Java

Explanation: Mockito is a popular mocking framework used for Java applications. It allows developers to write tests that isolate components from their dependencies by creating mock objects.

2. Which annotation is used to indicate a mock object in Mockito?

a) @Mockito
b) @Mock
c) @InjectMocks
d) @Spy

Answer: (b) @Mock

Explanation: The @Mock annotation is used to create a mock object in Mockito. This object is then used to simulate the behavior of a real object during testing.

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

a) To create a mock object
b) To verify that a method was called on a mock object
c) To inject dependencies into a test class
d) To assert that a value is equal to a given value

Answer: (b) To verify that a method was called on a mock object

Explanation: The verify() method in Mockito is used to verify that a method was called on a mock object. It can also be used to verify the number of times a method was called, and the order in which methods were called.

4. Which method is used to stub a method in Mockito?

a) when()
b) then()
c) doReturn()
d) all of the above

Answer: (d) all of the above

Explanation: Mockito provides several methods for stubbing methods, including when(), then(), and doReturn(). These methods can be used to specify the behavior of a mock object during testing.

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

a) A mock object can verify that a method was called, while a stub object cannot
b) A mock object is created using the @Mock annotation, while a stub object is created using the @Stub annotation
c) A mock object simulates the behavior of a real object, while a stub object provides a predefined response to a method call
d) There is no difference between a mock object and a stub object in Mockito

Answer: (c) A mock object simulates the behavior of a real object, while a stub object provides a predefined response to a method call

Explanation: A mock object is used to simulate the behavior of a real object during testing, and can also be used to verify that a method was called. A stub object, on the other hand, provides a predefined response to a method call, and is used to isolate a component from its dependencies during testing.

6. Which of the following is true about the @InjectMocks annotation in Mockito?
a) It is used to create a mock object
b) It is used to inject dependencies into a test class
c) It is used to verify that a method was called on a mock object
d) It is not a valid annotation in Mockito

Answer: (b) It is used to inject dependencies into a test class

Explanation: The @InjectMocks annotation is used to inject dependencies into a test class in Mockito. It can be used to automatically create and inject mock objects into a test class.

7. Which of the following is true about the @Spy annotation in Mockito?

a) It is used to create a mock object
b) It is used to inject dependencies into a test class
c) It is used to verify that a method was called on a mock object
d) It is used to create a spy object, which is a partial mock object

Answer: (d) It is used to create a spy object, which is a partial mock object

Explanation: The @Spy annotation is used to create a spy object in Mockito. A spy object is a partial mock object that allows you to stub some methods while calling the real implementation of others.

8. What is the difference between a mock object and a dummy object in Mockito?

a) A mock object simulates the behavior of a real object, while a dummy object does not
b) A mock object is created using the @Mock annotation, while a dummy object is created using the @Dummy annotation
c) A mock object is used to verify that a method was called, while a dummy object is not
d) There is no difference between a mock object and a dummy object in Mockito

Answer: (a) A mock object simulates the behavior of a real object, while a dummy object does not

Explanation: A mock object is used to simulate the behavior of a real object during testing, while a dummy object is typically used to provide a placeholder object for a test that requires an object but does not need to interact with it.

9. Which of the following is true about the doNothing() method in Mockito?
a) It is used to specify the behavior of a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to do nothing when a method is called on a mock object
d) It is not a valid method in Mockito

Answer: (c) It is used to do nothing when a method is called on a mock object

Explanation: The doNothing() method in Mockito is used to specify that a mock object should do nothing when a particular method is called. This can be useful for testing methods that do not have any side effects.

10. Which of the following is true about the reset() method in Mockito?

a) It is used to create a new mock object
b) It is used to reset the state of a mock object
c) It is used to verify that a method was called on a mock object
d) It is not a valid method in Mockito

Answer: (b) It is used to reset the state of a mock object

Explanation: The reset() method in Mockito is used to reset the state of a mock object. This can be useful if you need to reuse a mock object in multiple tests, or if you need to reset the state of a mock object between test runs.

11. Which of the following is true about the any() method in Mockito?

a) It is used to specify the behavior of a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to match any argument of a particular type
d) It is not a valid method in Mockito

Answer: (c) It is used to match any argument of a particular type

Explanation: The any() method in Mockito is used to match any argument of a particular type. This can be useful when you need to stub a method that takes an argument, but the value of the argument is not important for the test.

12. Which of the following is true about the ArgumentCaptor class in Mockito?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to capture the arguments of a method call on a mock object
d) It is not a valid class in Mockito

Answer: (c) It is used to capture the arguments of a method call on a mock object

Explanation: The ArgumentCaptor class in Mockito is used to capture the arguments of a method call on a mock object. This can be useful for verifying that the correct arguments were passed to a method during testing.

13. Which of the following is true about the verifyNoMoreInteractions() method in Mockito?

a) It is used to verify that a method was called on a mock object
b) It is used to verify that no more methods were called on a mock object after a specified point
c) It is used to reset the state of a mock object
d) It is not a valid method in Mockito

Answer: (b) It is used to verify that no more methods were called on a mock object after a specified point

Explanation: The verifyNoMoreInteractions() method in Mockito is used to verify that no more methods were called on a mock object after a specified point in the test. This can be useful for ensuring that all necessary interactions with the mock object have been completed.

14. Which of the following is true about the @Captor annotation in Mockito?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to capture the arguments of a method call on a mock object
d) It is not a valid annotation in Mockito

Answer: (c) It is used to capture the arguments of a method call on a mock object

Explanation: The @Captor annotation in Mockito is used to create an ArgumentCaptor object, which can be used to capture the arguments of a method call on a mock object. This can be useful for verifying that the correct arguments were passed to a method during testing.

15. Which of the following is true about the verifyZeroInteractions() method in Mockito?

a) It is used to verify that a method was called on a mock object
b) It is used to verify that no methods were called on a mock object
c) It is used to reset the state of a mock object
d) It is not a valid method in Mockito

Answer: (b) It is used to verify that no methods were called on a mock object

Explanation: The verifyZeroInteractions() method in Mockito is used to verify that no methods were called on a mock object during the test. This can be useful for ensuring that a particular interaction with the mock object did not occur.

16. Which of the following is true about the @RunWith(MockitoJUnitRunner.class) annotation in Mockito?

a) It is used to create a mock object
b) It is used to inject mock objects into a class being tested
c) It is used to verify that a method was called on a mock object
d) It is used to run JUnit tests with Mockito support

Answer: (d) It is used to run JUnit tests with Mockito support

Explanation: The @RunWith(MockitoJUnitRunner.class) annotation in Mockito is used to run JUnit tests with Mockito support. This can be useful for testing classes that use Mockito.

17. Which of the following is true about the @MockBean annotation in Spring Boot?
a) It is used to create a mock object
b) It is used to inject mock objects into a Spring Boot application
c) It is used to verify that a method was called on a mock object
d) It is not a valid annotation in Spring Boot

Answer: (b) It is used to inject mock objects into a Spring Boot application

Explanation: The @MockBean annotation in Spring Boot is used to inject mock objects into a Spring Boot application. This can be useful for testing Spring Boot applications that have dependencies on other objects.

18. Which of the following is true about the thenReturn() method in Mockito?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the return value of a method call on a mock object
d) It is not a valid method in Mockito

Answer: (c) It is used to specify the return value of a method call on a mock object

Explanation: The thenReturn() method in Mockito is used to specify the return value of a method call on a mock object. This can be useful for testing methods that have dependencies on other objects.

19. Which of the following is true about the verify() method in Mockito?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the behavior of a method call on a mock object
d) It is not a valid method in Mockito

Answer: (b) It is used to verify that a method was called on a mock object

Explanation: The verify() method in Mockito is used to verify that a method was called on a mock object during the test. This can be useful for ensuring that the correct interactions with the mock object occurred during the test.

20. Which of the following is true about the @Mock annotation in Mockito?

a) It is used to create a mock object
b) It is used to inject mock objects into a class being tested
c) It is used to verify that a method was called on a mock object
d) It is not a valid annotation in Mockito

Answer: (a) It is used to create a mock object

Explanation: The @Mock annotation in Mockito is used to create a mock object. This can be useful for testing classes that have dependencies on other objects.

21. Which of the following is true about the when() method in Mockito?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the behavior of a method call on a mock object
d) It is not a valid method in Mockito

Answer: (c) It is used to specify the behavior of a method call on a mock object

Explanation: The when() method in Mockito is used to specify the behavior of a method call on a mock object. This can be useful for testing methods that have dependencies on other objects.

22. What is the purpose of using Mockito.spy()?

a) To create a new instance of a class
b) To mock the behavior of a class
c) To monitor the behavior of a class
d) To inject a mock object into a class

Answer: (c) To monitor the behavior of a class

Explanation: The Mockito.spy() method is used to create a spy object that can monitor the behavior of a real object. The spy object can be used to verify the behavior of the real object during a test.

23. Which of the following is true about the doThrow() method in Mockito?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the behavior of a method call on a mock object
d) It is used to throw an exception when a method is called on a mock object

Answer: (d) It is used to throw an exception when a method is called on a mock object

Explanation: The doThrow() method in Mockito is used to specify that an exception should be thrown when a method is called on a mock object. This can be useful for testing error-handling code.

24. Which of the following is true about the doAnswer() method in Mockito?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the behavior of a method call on a mock object
d) It is not a valid method in Mockito

Answer: (c) It is used to specify the behavior of a method call on a mock object

Explanation: The doAnswer() method in Mockito is used to specify the behavior of a method call on a mock object. This can be useful for testing methods that have dependencies on other objects.

25. Which of the following is true about the Mockito.when() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the behavior of a method call on a mock object
d) It is used to throw an exception when a method is called on a mock object

Answer: (c) It is used to specify the behavior of a method call on a mock object

Explanation: The Mockito.when() method is used to specify the behavior of a method call on a mock object. This can be useful for testing methods that have dependencies on other objects.

26. Which of the following is true about the Mockito.verify() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the behavior of a method call on a mock object
d) It is used to throw an exception when a method is called on a mock object

Answer: (b) It is used to verify that a method was called on a mock object

Explanation: The Mockito.verify() method is used to verify that a method was called on a mock object during a test. This can be useful for testing the behavior of an object in response to a method call.

27. Which of the following is true about the Mockito.times() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the behavior of a method call on a mock object
d) It is used to throw an exception when a method is called on a mock object

Answer: (b) It is used to verify that a method was called on a mock object

Explanation: The Mockito.times() method is used to specify the number of times a method should be called on a mock object during a test. This can be useful for testing the behavior of an object in response to multiple method calls.

28. Which of the following is true about the Mockito.never() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the behavior of a method call on a mock object
d) It is used to verify that a method was not called on a mock object

Answer: (d) It is used to verify that a method was not called on a mock object

Explanation: The Mockito.never() method is used to verify that a method was not called on a mock object during a test. This can be useful for testing that certain conditions are not met during a test.

29. Which of the following is true about the Mockito.atLeast() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the behavior of a method call on a mock object
d) It is used to specify the minimum number of times a method should be called on a mock object during a test

Answer: (d) It is used to specify the minimum number of times a method should be called on a mock object during a test

Explanation: The Mockito.atLeast() method is used to specify the minimum number of times a method should be called on a mock object during a test. This can be useful for testing the behavior of an object in response to multiple method calls.

30. Which of the following is true about the Mockito.atMost() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the behavior of a method call on a mock object
d) It is used to specify the maximum number of times a method should be called on a mock object during a test

Answer: (d) It is used to specify the maximum number of times a method should be called on a mock object during a test

Explanation: The Mockito.atMost() method is used to specify the maximum number of times a method should be called on a mock object during a test. This can be useful for testing the behavior of an object in response to multiple method calls.

31. Which of the following is true about the Mockito.timeout() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the behavior of a method call on a mock object
d) It is used to specify the maximum time that should be spent waiting for a method call on a mock object

Answer: (d) It is used to specify the maximum time that should be spent waiting for a method call on a mock object

Explanation: The Mockito.timeout() method is used to specify the maximum time that should be spent waiting for a method call on a mock object during a test. This can be useful for testing the behavior of an object in response to a method call that may take some time to complete.

32. Which of the following is true about the Mockito.reset() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to reset the behavior of a mock object
d) It is used to throw an exception when a method is called on a mock object

Answer: (c) It is used to reset the behavior of a mock object

Explanation: The Mockito.reset() method is used to reset the behavior of a mock object. This can be useful for testing different scenarios with the same mock object without having to create a new one each time.

33. Which of the following is true about the Mockito.mockingDetails() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to retrieve information about a mock object
d) It is used to throw an exception when a method is called on a mock object

Answer: (c) It is used to retrieve information about a mock object

Explanation: The Mockito.mockingDetails() method is used to retrieve information about a mock object, such as the number of times a method was called on the object during a test.

34. Which of the following is true about the Mockito.doReturn() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify the behavior of a method call on a mock object
d) It is used to throw an exception when a method is called on a mock object

Answer: (c) It is used to specify the behavior of a method call on a mock object

Explanation: The Mockito.doReturn() method is used to specify the behavior of a method call on a mock object.

35. Which of the following is true about the Mockito.doThrow() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify that a method call on a mock object should throw an exception
d) It is used to specify the behavior of a method call on a mock object

Answer: (c) It is used to specify that a method call on a mock object should throw an exception

Explanation: The Mockito.doThrow() method is used to specify that a method call on a mock object should throw an exception. This can be useful for testing the behavior of an object in response to exceptional situations.

36. Which of the following is true about the Mockito.doAnswer() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify a custom behavior for a method call on a mock object
d) It is used to specify that a method call on a mock object should throw an exception

Answer: (c) It is used to specify a custom behavior for a method call on a mock object

Explanation: The Mockito.doAnswer() method is used to specify a custom behavior for a method call on a mock object. This can be useful for testing complex scenarios where a simple return value or exception is not sufficient.

37. Which of the following is true about the Mockito.doNothing() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify that a method call on a mock object should do nothing
d) It is used to specify a custom behavior for a method call on a mock object

Answer: (c) It is used to specify that a method call on a mock object should do nothing

Explanation: The Mockito.doNothing() method is used to specify that a method call on a mock object should do nothing. This can be useful for testing scenarios where a method call has side effects but those effects are not relevant to the test.

38. Which of the following is true about the Mockito.doCallRealMethod() method?

a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to specify that a method call on a mock object should call the real implementation of the method
d) It is used to specify a custom behavior for a method call on a mock object

Answer: (c) It is used to specify that a method call on a mock object should call the real implementation of the method

Explanation: The Mockito.doCallRealMethod() method is used to specify that a method call on a mock object should call the real implementation of the method. This can be useful for testing scenarios where the behavior of the real implementation is important to the test.

39. Which of the following is true about the Mockito.verifyNoMoreInteractions() method?
a) It is used to create a mock object
b) It is used to verify that a method was called on a mock object
c) It is used to verify that no more interactions occurred on a mock object after a specified point
d) It is used to specify a custom behavior for a method call on a mock object

Answer: (c) It is used to verify that no more interactions occurred on a mock object after a specified point

Explanation: The Mockito.verifyNoMoreInteractions() method is used to verify that no more interactions occurred on a mock object after a specified point in a test. This can be useful for ensuring that all relevant behavior

40. Which of the following is true about the Mockito.spy() method?

a) It is used to create a mock object
b) It is used to create a real object that can be partially mocked
c) It is used to verify that a method was called on a mock object
d) It is used to specify a custom behavior for a method call on a mock object

Answer: (b) It is used to create a real object that can be partially mocked

Explanation: The Mockito.spy() method is used to create a real object that can be partially mocked. This can be useful for testing scenarios where the behavior of some methods on the real object is important, but others should be mocked.

Hope by going through these Mockito MCQs with Answers you got to know that Mockito is a powerful and widely used testing framework in Java that allows developers to easily create mock objects and test their code in isolation. Keep browsing our Freshersnow website in your free time to gather such interesting updates.

Freshersnow.com is one of the best job sites in India. On this website you can find list of jobs such as IT jobs, government jobs, bank jobs, railway jobs, work from home jobs, part time jobs, online jobs, pharmacist jobs, software jobs etc. Along with employment updates, we also provide online classes for various courses through our android app. Freshersnow.com also offers recruitment board to employers to post their job advertisements for free.