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
2. How can you create a mock object using Mockito?
a) Mock.create()
b) Mockito.mock()
c) Mockito.createMock()
d) Mock.new()
3. Which annotation is used to inject mock objects?
a) @MockBean
b) @Mock
c) @InjectMock
d) @InjectMocks
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
5. How do you specify the number of times a mock method should be called?
a) times()
b) calls()
c) invokeCount()
d) frequency()
6. Which method allows a mock to throw an exception when it's called?
a) doThrow()
b) throwWhen()
c) onInvokeThrow()
d) triggerException()
7. How do you make a mock return a specific value when called?
a) returnValue()
b) returnWhenCalled()
c) when().thenReturn()
d) onInvokeReturn()
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
9. Which of the following is used to create a partial mock with Mockito?
a) partialMock()
b) spy()
c) halfMock()
d) mockPartial()
10. Which method is used to reset a mock in Mockito?
a) Mockito.reset()
b) Mockito.restart()
c) Mockito.clear()
d) Mockito.refresh()
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
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.
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()
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.
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.
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.
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.
18. How can you ensure that no unwanted methods are called on a mock?
a) verifyNoMoreInteractions()
b) verifyZeroInteractions()
c) verifyOnly()
d) ensureNoInteractions()
19. Which of the following can't be mocked using Mockito by default?
a) Interfaces
b) Abstract classes
c) Private methods
d) Final classes
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)
Comments
Post a Comment
Leave Comment