Junior — Middle
What tool did you use for testing with mocks or stubs?
sobes.tech AI
Answer from AI
Specialized libraries are often used for testing with mocks and stubs, allowing to simulate the behavior of dependencies and control interactions.
Popular tools:
- Mockito — a widely used framework for Java, allows creating mocks and stubs, checking method calls.
- JUnit + Mockito — often used together for writing unit tests.
- EasyMock — an alternative to Mockito for Java.
- Sinon.js — for JavaScript.
- unittest.mock — a built-in module in Python.
Example with Mockito:
import static org.mockito.Mockito.*;
List<String> mockedList = mock(List.class);
when(mockedList.get(0)).thenReturn("first element");
System.out.println(mockedList.get(0)); // Will output "first element"
verify(mockedList).get(0);