2.3 KiB
vaessl: Test strategy
Spring Boot Test Environment
-
Environment Parity
Instead of testing against a mocked database or the development database, I created a mirrored test container. This allows the application to perform JPA operations against a real PostgreSQL instance that matches the production engine but uses a different port (5434) and database name (vaessl_test).
-
Profile-Driven Configuration
I utilized the Spring Profiles mechanism. By tagging the test class with @ActiveProfiles("test"), Spring Boot ignores the standard application.yaml for specific keys and prioritizes src/test/resources/application-test.yaml.
The configuration is moved out of the Java code and into YAML. This follows the "Separation of Concerns" principle.
The file is placed in src/test/resources (a sibling to src/test/java). This is the standard Gradle "SourceSet" layout, ensuring the build tool automatically packages these settings only during the test phase.
-
Gradle Test Lifecycle
I corrected the dependency graph in build.gradle.kts. While the Initializr provides "test slices" (like data-jpa-test), a full integration test requires the Spring Boot Starter Test core.
This starter provides the YAML parsing engine and the SpringBootContextLoader.
Frontend Test environment
-
The Vitest Stack
-
Vitest: providing a unified configuration for both the development server and the test runner.
-
jsdom: Provides a lightweight browser environment in Node.js.
-
React Testing Library: Ensures tests are written from the user's perspective (DOM-based) rather than implementation details.
-
-
Dynamic Environment Configuration
The vitest.config.ts is configured to be environment-aware. By using loadEnv, the test runner can access variables from .env files, allowing us to:
-
Dynamically set the Server Port (51204).
-
Handle Allowed Hosts (crucial for code-server or LXC environments where the public URL might change).
-
Inject environment-specific keys into the test.env context.
-
-
TDD Workflow (Red-Green-Refactor)
The environment is optimized for a fast feedback loop.
-
npm run test: Runs Vitest in "Watch Mode," re-triggering tests instantly upon file changes.
-
npm run test:ui: Launches the Vitest UI, providing a visual graph of test suites and real-time failure reports.
-