Junior — Middle
Describe your experience with mock objects in testing.
sobes.tech AI
Answer from AI
Mock objects are widely used in automated testing to isolate the tested component from external dependencies.
Usage experience includes:
- Replacing real dependencies (e.g., databases, web services) with mocks to control behavior and speed up tests.
- Configuring mocks to return specific values or generate exceptions to test different scenarios.
- Verifying calls to mock methods (e.g., how many times and with what parameters a method was called).
Example using Mockito (Java):
// Creating a mock
List<String> mockedList = Mockito.mock(List.class);
// Configuring behavior
Mockito.when(mockedList.get(0)).thenReturn("first element");
// Using
String element = mockedList.get(0); // Will return "first element"
// Verifying call
Mockito.verify(mockedList).get(0);
Thus, mocks help write reliable and fast unit tests, focusing on the logic of the tested code rather than external services.