Mockito MCQ - Multiple Choice Questions and Answers

Testing is a cornerstone of software development, ensuring that the software behaves as expected and providing a safety net for future changes. Mockito is a popular testing framework in the Java ecosystem that focuses on mock creation, verification, and stubbing. If you're a beginner to Mockito or just want to test your knowledge, this set of Multiple Choice Questions (MCQs) is for you!

1. Which of the following best describes Mockito?

a) A unit testing framework
b) A Java performance monitoring tool
c) A mocking framework for unit tests in Java
d) A Java compiler

Answer:

c) A mocking framework for unit tests in Java

Explanation:

Mockito is a framework that aids in creating and using mock objects in Java unit tests.

2. How can you create a mock object using Mockito?

a) Mock.create()
b) Mockito.mock()
c) Mockito.createMock()
d) Mock.new()

Answer:

b) Mockito.mock()

Explanation:

The mock() method of Mockito is used to create mock objects.

3. Which annotation is used to inject mock objects?

a) @MockBean
b) @Mock
c) @InjectMock
d) @InjectMocks

Answer:

d) @InjectMocks

Explanation:

The @InjectMocks annotation is used to inject mock objects into the tested object.

4. What does the verify() method do in Mockito?

a) Creates a mock object
b) Checks if a method was called with certain parameters
c) Injects mock objects
d) Creates a spy object

Answer:

b) Checks if a method was called with certain parameters

Explanation:

The verify() method is used to check if certain interactions with the mock object have taken place.

5. How do you specify the number of times a mock method should be called?

a) times()
b) calls()
c) invokeCount()
d) frequency()

Answer:

a) times()

Explanation:

The times() method is used in combination with verify() to check how many times a mock method has been called.

6. Which method allows a mock to throw an exception when it's called?

a) doThrow()
b) throwWhen()
c) onInvokeThrow()
d) triggerException()

Answer:

a) doThrow()

Explanation:

The doThrow() method is used to make a mock method throw an exception when it is called.

7. How do you make a mock return a specific value when called?

a) returnValue()
b) returnWhenCalled()
c) when().thenReturn()
d) onInvokeReturn()

Answer:

c) when().thenReturn()

Explanation:

The pattern when(someMock.someMethod()).thenReturn(value) is used to stub a method call to return a specific value.

8. What does Mockito's any() method do?

a) Checks if any mock method was called
b) Matches any value of any type
c) Verifies that all mock methods were called
d) Always returns true

Answer:

b) Matches any value of any type

Explanation:

any() is an argument matcher that matches any value of any type.

9. Which of the following is used to create a partial mock with Mockito?

a) partialMock()
b) spy()
c) halfMock()
d) mockPartial()

Answer:

b) spy()

Explanation:

The spy() method is used to create a partial mock, where some methods are mocked while others retain their original behavior.

10. Which method is used to reset a mock in Mockito?

a) Mockito.reset()
b) Mockito.restart()
c) Mockito.clear()
d) Mockito.refresh()

Answer:

a) Mockito.reset()

Explanation:

Mockito.reset() is used to reset a mock so that it forgets all interactions and stubbings.

11. How can you ensure that a mock method was never called in Mockito?

a) verify(mock, never())
b) verifyZeroInteractions(mock)
c) verifyNoMoreInteractions(mock)
d) Both a and b

Answer:

d) Both a and b

Explanation:

Both verify(mock, never()) and verifyZeroInteractions(mock) can be used to ensure that a mock method was never called.

12. What is the purpose of @Captor annotation in Mockito?

a) It captures mock method arguments for further assertions.
b) It captures the return value of a mock method.
c) It captures any exceptions thrown by the mock.
d) It captures the state of the mock before testing.

Answer:

a) It captures mock method arguments for further assertions.

Explanation:

@Captor is used in conjunction with ArgumentCaptor to capture arguments passed to mock methods for further assertions.

13. Which of the following Mockito methods is used to stub consecutive calls to a mock?

a) when().thenChainReturn()
b) when().thenReturnConsecutive()
c) when().thenAnswer()
d) when().thenReturn().thenReturn()

Answer:

d) when().thenReturn().thenReturn()

Explanation:

To stub consecutive calls, you can chain multiple thenReturn() methods.

14. What does doAnswer() method allow you to do?

a) It provides a default answer for all mock methods.
b) It allows specifying custom answers or actions to mock calls.
c) It answers with the actual method call.
d) It always throws an exception.

Answer:

b) It allows specifying custom answers or actions to mock calls.

Explanation:

doAnswer() allows you to specify a custom Answer which can contain custom logic or actions to be executed when a mock method is called.

15. What is the purpose of verifyNoInteractions() in Mockito?

a) Ensure some specific methods of the mock were not called.
b) Ensure no methods of the mock were called at all.
c) Ensure that mock was called a specific number of times.
d) Reset the mock interactions.

Answer:

b) Ensure no methods of the mock were called at all.

Explanation:

verifyNoInteractions() checks that no methods of the mock were called, ensuring the mock had zero interactions.

16. How can you mock the behavior of void methods in Mockito?

a) Using when().thenVoid()
b) Using doNothing().when(mock)
c) Using mock().voidMethod()
d) Void methods cannot be mocked.

Answer:

b) Using doNothing().when(mock)

Explanation:

For void methods, you typically use the pattern doNothing().when(mock).voidMethod() to mock their behavior.

17. Which of the following is true about the difference between spy and mock in Mockito?

a) spy creates a real object, whereas mock creates a dummy object.
b) spy always calls the real method, while mock never does.
c) There is no difference; both can be used interchangeably.
d) spy cannot be used for stubbing, while mock can.

Answer:

a) spy creates a real object, whereas mock creates a dummy object.

Explanation:

While both spy and mock can be used for stubbing, the key difference is that a spy wraps a real object and retains its original behavior unless specified otherwise, while a mock creates a dummy object without any behavior.

18. How can you ensure that no unwanted methods are called on a mock?

a) verifyNoMoreInteractions()
b) verifyZeroInteractions()
c) verifyOnly()
d) ensureNoInteractions()

Answer:

a) verifyNoMoreInteractions()

Explanation:

verifyNoMoreInteractions() checks that no methods other than the ones you've verified were called on the mock.

19. Which of the following can't be mocked using Mockito by default?

a) Interfaces
b) Abstract classes
c) Private methods
d) Final classes

Answer:

d) Final classes

Explanation:

By default, Mockito can't mock final classes. However, this can be circumvented using configurations or other libraries.

20. How can you return different values on successive calls to a mock method in Mockito?

a) when(mock.method()).thenReturn(value1, value2, value3)
b) when(mock.method()).thenReturnMultiple(value1, value2)
c) doReturn(value1).doReturn(value2).when(mock.method())
d) when(mock.method()).returnSeries(value1, value2)

Answer:

a) when(mock.method()).thenReturn(value1, value2, value3)

Explanation:

To return different values on successive calls, you can pass multiple values to the thenReturn() method.


Comments