Sobes.tech
Junior — Middle

Have you used Mockito for testing hidden or internally implicit system components?

sobes.tech AI

Answer from AI

Yes, Mockito is often used for testing hidden or internal components of a system, especially when it is necessary to isolate the tested class from dependencies.

For example, if a class depends on a private service or an internal component that is difficult or undesirable to initialize in a test, Mockito allows creating a mock object of that component and setting its behavior.

Example of using Mockito to mock a dependency:

public class UserService {
    private UserRepository userRepository;

    public UserService(UserRepository repo) {
        this.userRepository = repo;
    }

    public User getUserById(int id) {
        return userRepository.findById(id);
    }
}

// In the test
UserRepository mockRepo = Mockito.mock(UserRepository.class);
Mockito.when(mockRepo.findById(1)).thenReturn(new User("John"));

UserService service = new UserService(mockRepo);
User user = service.getUserById(1);

assertEquals("John", user.getName());

Thus, Mockito helps test logic that does not depend on the actual implementations of internal components.

Have you used Mockito for testing hidden or… - sobes.tech