Junior — Middle
Do you have experience creating mock objects for testing?
sobes.tech AI
Answer from AI
Mock objects are used in testing to imitate the behavior of real components, in order to isolate the tested module and verify its logic without dependence on external systems.
For example, if you have a class that accesses a database, you can create a mock database object that returns predefined data without executing real queries.
In popular frameworks like Mockito for Java, creating a mock looks like this:
// Create a mock object
List<String> mockedList = Mockito.mock(List.class);
// Set behavior
Mockito.when(mockedList.get(0)).thenReturn("First element");
// Use the mock
System.out.println(mockedList.get(0)); // Outputs: First element
This allows testing logic without real dependencies.