Sobes.tech
Junior — Middle

Kirjeldage oma kogemust kasutades mock-objekte testimisel

sobes.tech AI

Vastus AI-lt

Mock objects are widely used in automated testing to isolate the tested component from external dependencies.

Experience with their use 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 handling of different scenarios.
  • Verifying calls to mock methods (e.g., how many times and with what parameters the method was called).

Example with Mockito (Java):

// Creating a mock
List<String> mockedList = Mockito.mock(List.class);

// Setting 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);

In this way, mocks help write reliable and fast unit tests, focusing on the logic of the tested code, not on external services.