Testing Guide¶
This guide outlines the testing approaches and tools used in the geOrchestra Gateway project to help developers write effective tests.
Test Types¶
The project uses several types of tests:
- Unit Tests: Test individual components in isolation
- Integration Tests: Test component interactions within the application context
- End-to-End Tests: Test complete request-response cycles
Testing Framework¶
The Gateway uses:
- JUnit 5: Primary testing framework
- Spring Boot Test: For Spring-specific testing support
- Mockito: For mocking dependencies
- AssertJ: For fluent assertions
Test Structure¶
Tests should be placed in the src/test/java
directory, mirroring the package structure of the main code:
- Unit tests:
*Test.java
- Integration tests:
*IT.java
Writing Unit Tests¶
Unit tests should:
- Test a single unit of functionality
- Mock all dependencies
- Be fast and isolated
- Follow the Arrange-Act-Assert pattern
Example unit test:
@ExtendWith(MockitoExtension.class)
class GeorchestraUserMapperTest {
@Mock
private RolesMappingsUserCustomizer customizer;
@InjectMocks
private GeorchestraUserMapper mapper;
@Test
void testMappingBasicUser() {
// Arrange
Authentication authentication = mock(Authentication.class);
when(authentication.getPrincipal()).thenReturn("testuser");
// Act
GeorchestraUser user = mapper.mapUser(authentication);
// Assert
assertThat(user.getUsername()).isEqualTo("testuser");
}
}
Writing Integration Tests¶
Integration tests use Spring's test context to test component interactions:
@SpringBootTest
@ActiveProfiles("test")
class HeaderPreAuthenticationConfigurationIT {
@Autowired
private WebTestClient webClient;
@Test
void testPreAuthentication() {
webClient.get()
.uri("/whoami")
.header("sec-username", "testuser")
.exchange()
.expectStatus().isOk()
.expectBody()
.jsonPath("$.username").isEqualTo("testuser");
}
}
Test Configuration¶
Spring Boot test configuration can be defined in src/test/resources/application-test.yml
or through @TestConfiguration
classes.
Mock Objects¶
For LDAP, OAuth, and other external services, use mock implementations:
MockLdapServer
for LDAP testingMockWebServer
for OAuth endpoint testing
Testing Reactive Components¶
For testing reactive components:
- Use
StepVerifier
fromreactor-test
- Test both happy paths and error scenarios
- Verify completion signals
Example:
@Test
void testReactiveFlow() {
Flux<String> result = service.processData("test");
StepVerifier.create(result)
.expectNext("processed:test")
.verifyComplete();
}
Testing Security¶
For testing security components:
- Use
@WithMockUser
or custom security annotations - Test both authenticated and unauthenticated scenarios
- Verify proper authorization decisions
Test Data¶
Store test data in:
src/test/resources
for files- Test classes for small data objects
- Factories for complex test objects
Running Tests¶
Run all tests:
./mvnw test
Run integration tests:
./mvnw verify
Run a specific test:
./mvnw test -Dtest=GeorchestraUserMapperTest
Test Coverage¶
Use JaCoCo for test coverage:
./mvnw test jacoco:report
Coverage reports are generated in target/site/jacoco/index.html
.