Application testing

1. Overview

Testing in the Provision Portal project is structured to balance speed, reliability, and coverage. A combination of unit tests, integration tests, and process-level tests ensures confidence in business logic, service interaction, and external system integration.

Tests are organized by type and placed alongside the code they validate (typically in src/test/java). Each test type serves a specific purpose:

  • Unit Tests: Fast and isolated. Ideal for verifying business logic and utility methods.

  • Integration Tests: Validate how components interact in a realistic Spring Boot context.

2. Best Practices

  • Follow naming conventions like *Test or *IT to distinguish between unit and integration tests.

  • Use @MockBean or Testcontainers to isolate dependencies in integration tests.

  • Use @ActiveProfiles("test") to activate test-specific configurations.

  • Prefer readable assertions (assertThat, assertEquals, etc.) over excessive mocking.

  • Avoid external dependencies or shared state between tests unless explicitly required.

3. Coverage Goals

  • The project enforces a minimum of 80% code coverage via JaCoCo.

  • All new code should be accompanied by appropriate tests.

  • Focus on testing meaningful business logic, not just line coverage.

JaCoCo code coverage report showing test coverage metrics
Figure 1. Current JaCoCo Coverage Report

4. Unit Tests Example

  • Focus on individual components (e.g., service methods or delegates) in isolation.

  • Use @ExtendWith(MockitoExtension.class) for mocking dependencies with Mockito.

  • Example:

Unresolved include directive in modules/ROOT/pages/testing.adoc - include::../../src/test/java/com/friendly/provisionportal/provision/delegate/DeletePreviousByMacDelegateTest.java[]

5. Integration Tests Example

  • Verify collaboration of components with full Spring Boot context.

  • Use @SpringBootTest, @Sql, and @AutoConfigureMockMvc.

  • Testcontainers spin up MySQL in isolation.

  • Example base class:

Unresolved include directive in modules/ROOT/pages/testing.adoc - include::../../src/test/java/com/friendly/provisionportal/BaseIT.java[]

6. Running Tests

To execute tests and generate a coverage report, use the following command:

./gradlew clean test

Coverage report path:

build/reports/jacoco/test/html/index.html