Sobes.tech
Middle+

What tests do you write? What frameworks do you use?

sobes.tech AI

Answer from AI

I write several types of tests: unit tests to check individual methods and classes, integration tests to verify the interaction of components and services, and sometimes end-to-end tests to verify the entire application.

For unit testing in Java, I usually use JUnit 5 — it is a standard and powerful framework. For mocking dependencies, I use Mockito, which allows isolating the tested code. For integration tests, I often use Spring Boot Test if the project is on Spring, as it simplifies setting up the context and working with the database.

Here's an example of a simple unit test with JUnit and Mockito:

@ExtendWith(MockitoExtension.class)
class UserServiceTest {
    @Mock
    private UserRepository userRepository;

    @InjectMocks
    private UserService userService;

    @Test
    void testFindUserById() {
        User user = new User(1L, "John");
        when(userRepository.findById(1L)).thenReturn(Optional.of(user));

        User result = userService.findUserById(1L);

        assertEquals("John", result.getName());
    }
}