Junior
What is integration testing?
sobes.tech AI
Answer from AI
Integration testing is a type of software testing where individual modules are combined and tested as a group. The goal is to verify how components interact with each other and with external systems.
Main approaches:
- Top-Down: Testing starts from the top modules, with lower modules replaced by stubs.
- Bottom-Up: Testing begins with lower modules, with higher modules replaced by drivers.
- Hybrid: Combines both top-down and bottom-up approaches.
Examples of integration in Java:
- Testing service interaction with a database.
- Testing one REST API call from another.
- Testing component operation with message queue systems (e.g., Kafka).
Frameworks often used for JVM applications include Spring Boot Test, JUnit 5 with extensions (e.g., Testcontainers), Mockito (for mocking dependencies).
// Example of integration test with Spring Boot Test
@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private UserRepository userRepository; // Repository for DB operations
@BeforeEach
void setup() {
userRepository.deleteAll(); // Clear database before test
}
@Test
void testCreateUser() throws Exception {
String userJson = "{\"username\": \"testUser\", \"email\": \"test@example.com\"}";
mockMvc.perform(post("/users") // Call controller
.contentType(MediaType.APPLICATION_JSON)
.content(userJson))
.andExpect(status().isCreated()) // Expect 201 status
.andExpect(jsonPath("$.username").value("testUser")); // Check response content
assertThat(userRepository.findByUsername("testUser")).isPresent(); // Check DB
}
}
| Advantages | Disadvantages |
|---|---|
| Detects interaction errors between modules. | More complex to write and maintain than unit tests. |
| Helps verify external system interactions. | Longer execution time. |
| Closer to real usage scenarios. | Requires environment setup (DB, services, etc.). |