Sobes.tech
Junior — Middle

Have you had experience using Mockito for writing tests?

sobes.tech AI

Answer from AI

Yes, I have used Mockito for writing unit tests in Java. Mockito allows creating mock objects — imitations of real dependencies, to isolate the class under test and verify its behavior.

Example of using Mockito:

import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;

class UserServiceTest {
    @Test
    void testGetUserName() {
        UserRepository mockRepo = mock(UserRepository.class);
        when(mockRepo.findNameById(1)).thenReturn("Ivan");

        UserService service = new UserService(mockRepo);
        String name = service.getUserName(1);

        assertEquals("Ivan", name);
        verify(mockRepo).findNameById(1);
    }
}

Thus, Mockito helps replace real dependencies and verify interactions with them.