Junior — Middle
How do you perform testing of the server-side of your applications?
sobes.tech AI
Answer from AI
Server-side testing typically includes several levels:
- Unit tests — test individual methods and classes in isolation from external dependencies. Frameworks like JUnit, Mockito are used.
- Integration tests — test the interaction between components, such as controllers, services, and repositories together. Spring Test, Testcontainers are often used to set up a test database.
- End-to-End tests — test the entire system, including APIs. Tools like Postman, REST-assured, or Selenium can be used for UI testing.
Example of a unit test in Java using JUnit and Mockito:
@RunWith(MockitoJUnitRunner.class)
public class UserServiceTest {
@Mock
private UserRepository userRepository;
@InjectMocks
private UserService userService;
@Test
public void testGetUserById() {
User user = new User(1L, "John");
Mockito.when(userRepository.findById(1L)).thenReturn(Optional.of(user));
User result = userService.getUserById(1L);
assertEquals("John", result.getName());
}
}
It is important to automate tests and run them in CI/CD for server stability.