1 Integration Testing
Jan K9f edited this page 2025-06-04 09:36:55 +02:00

Integration Testing

This page covers the integration testing strategy for the Casino application.

Overview

Integration tests verify that different components of the system work together correctly. They test the interaction between:

  • Backend services
  • Database access
  • External APIs
  • Frontend components (where applicable)

Backend Integration Tests

Backend integration tests focus on testing the interaction between multiple backend components and external services.

Spring Boot Integration Tests

@SpringBootTest
@AutoConfigureMockMvc
public class GameServiceIntegrationTest {

    @Autowired
    private MockMvc mockMvc;
    
    @MockBean
    private ExternalGameProvider externalGameProvider;
    
    @Test
    public void testCreateGame() throws Exception {
        // Arrange
        when(externalGameProvider.initialize()).thenReturn(true);
        
        // Act & Assert
        mockMvc.perform(post("/api/games")
                .contentType(MediaType.APPLICATION_JSON)
                .content("{\"gameType\":\"SLOTS\",\"betAmount\":10}"))
                .andExpect(status().isCreated())
                .andExpect(jsonPath("$.gameId").exists());
    }
}

End-to-End Testing

E2E tests simulate real user scenarios across the entire application stack.

Running E2E Tests

# Backend and frontend E2E tests
./gradlew e2eTest

Database Integration

Tests involving database operations:

@DataJpaTest
public class UserRepositoryTest {

    @Autowired
    private UserRepository userRepository;
    
    @Test
    public void testFindByUsername() {
        // Arrange
        User user = new User();
        user.setUsername("testuser");
        user.setEmail("test@example.com");
        userRepository.save(user);
        
        // Act
        User found = userRepository.findByUsername("testuser");
        
        // Assert
        assertNotNull(found);
        assertEquals("test@example.com", found.getEmail());
    }
}

API Contract Testing

Using Spring Cloud Contract to ensure API compatibility:

Contract.make {
    description "Should return a user by ID"
    
    request {
        method GET()
        url "/api/users/1"
    }
    
    response {
        status 200
        headers {
            contentType applicationJson()
        }
        body([
            "id": 1,
            "username": "testuser",
            "email": "test@example.com"
        ])
    }
}

Test Data Management

Integration tests should:

  1. Set up their own test data
  2. Clean up after test execution
  3. Be independent and idempotent