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?
Answer:
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?
Answer:
Explanation:
The mock() method of Mockito is used to create mock objects.
3. Which annotation is used to inject mock objects?
Answer:
Explanation:
The @InjectMocks annotation is used to inject mock objects into the tested object.
4. What does the verify() method do in Mockito?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
To stub consecutive calls, you can chain multiple thenReturn() methods.
14. What does doAnswer() method allow you to do?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
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?
Answer:
Explanation:
To return different values on successive calls, you can pass multiple values to the thenReturn() method.
Comments
Post a Comment
Leave Comment