Skip to main content

Feature 10: Test Infrastructure

Purpose: This guide provides step-by-step instructions for implementing clean test execution infrastructure.

Dependencies: No dependencies on other Epic 2 features. Can be implemented independently.

Related Documents:


Overview

What This Guide Covers

Test Infrastructure provides clean test execution setup:

  • Test profile Flyway disable configuration
  • Clean test execution setup

What's Included:

  • application.yml test profile configuration
  • Flyway disable in test profile

What's NOT Included:

  • ❌ Test database setup (already in Springular)
  • ❌ Test framework configuration (already in Springular)
  • ❌ Test utilities (already in Springular)

Prerequisites

  • Springular boilerplate setup
  • Understanding of Spring Boot profiles
  • Understanding of Flyway migrations
  • Understanding of H2 test database

Implementation Steps

Step 1: Configure Test Profile to Disable Flyway

File: server/src/main/resources/application.yml

Purpose: Disable Flyway in test profile to allow H2 to auto-create schema.

Key Points:

  • ✅ H2 auto-creates schema in tests
  • ✅ Flyway disabled in test profile
  • ✅ Clean separation of concerns
  • ✅ Faster test execution

Find or create test profile section and add:

---
spring:
config:
activate:
on-profile: 'test'

flyway:
enabled: false # Disable Flyway in tests - let H2 auto-create schema

Complete test profile configuration example:

---
spring:
config:
activate:
on-profile: 'test'

datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: sa
password:

jpa:
hibernate:
ddl-auto: create-drop # Let H2 auto-create schema
database-platform: org.hibernate.dialect.H2Dialect

flyway:
enabled: false # Disable Flyway in tests - let H2 auto-create schema

Configuration Explanation:

  • on-profile: 'test' - Activates this configuration for test profile
  • flyway.enabled: false - Disables Flyway migrations in tests
  • hibernate.ddl-auto: create-drop - H2 auto-creates schema from entities
  • H2 in-memory database for tests

Why Disable Flyway in Tests:

  • H2 auto-creates schema from JPA entities (faster)
  • Prevents Flyway migration conflicts in tests
  • Faster test execution (no migration overhead)
  • Clean separation of concerns (tests use H2 auto-creation, production uses Flyway)

Verification

Step 1: Verify Test Profile Configuration

Run tests with test profile:

./gradlew test --args='--spring.profiles.active=test'

Or run tests normally (Spring Boot Test automatically uses test profile):

./gradlew test

Expected Result:

  • Tests run successfully
  • Flyway does not run during tests
  • H2 auto-creates schema from entities
  • No migration errors

Step 2: Verify Flyway is Disabled in Tests

Check test logs:

  • Look for Flyway migration messages
  • Expected: No Flyway migration messages in test logs

Verify H2 Schema Creation:

  • Check logs for Hibernate schema creation messages
  • Expected: Hibernate creates schema from entities

Step 3: Verify Existing Tests Still Pass

Run all tests:

./gradlew test

Expected Result:

  • All existing tests pass
  • No Flyway-related errors
  • Test execution is clean

Testing

Unit Tests

No unit tests required (configuration change).

Integration Tests

Verify test profile works:

  • Integration tests use test profile
  • Flyway is disabled in tests
  • H2 auto-creates schema
  • Tests execute successfully

Verify production profile works:

  • Production uses Flyway (not disabled)
  • Migrations run in production
  • Schema is managed by Flyway

Troubleshooting

Flyway Still Runs in Tests

Issue: Flyway migrations still run during tests.

Solution:

  1. Verify test profile is activated (@ActiveProfiles("test") in test classes or automatic)
  2. Check flyway.enabled: false is in test profile section
  3. Verify profile section uses --- separator
  4. Check test configuration uses test profile

Schema Creation Errors in Tests

Issue: H2 fails to create schema from entities.

Solution:

  1. Verify hibernate.ddl-auto: create-drop or update is set in test profile
  2. Check entity annotations are correct
  3. Verify H2 dialect is configured
  4. Check for entity mapping errors

Tests Fail After Adding New Entities

Issue: Tests fail when new entities are added.

Solution:

  1. Verify H2 auto-creation is working
  2. Check entity annotations are correct
  3. Verify test profile configuration
  4. Check for missing entity relationships or constraints

Best Practices

Test Profile Configuration

Keep test profile minimal:

  • Only override what's necessary for tests
  • Use H2 in-memory database
  • Disable Flyway (let H2 auto-create schema)
  • Use create-drop for clean state per test

Production vs Test

Production:

  • Uses Flyway migrations
  • Uses PostgreSQL
  • Schema managed by Flyway

Test:

  • Flyway disabled
  • Uses H2 in-memory
  • Schema auto-created from entities

Clear Separation:

  • Test profile explicitly disables Flyway
  • Production relies on Flyway
  • No confusion about schema management

Next Steps

After completing this guide:

All Epic 2 implementation guides are complete!

  1. Database Schema & Validation - Complete
  2. Production Infrastructure - Complete
  3. Code Quality & Developer Experience - Complete
  4. Code Generation Infrastructure - Complete
  5. Test Infrastructure - Complete

Ready for implementation!


Summary

This implementation provides:

  • Clean Test Execution: Flyway disabled in tests
  • Faster Tests: H2 auto-creates schema (no migration overhead)
  • Clear Separation: Test vs production configuration
  • Reliable Tests: No Flyway migration conflicts

This is a medium priority enhancement that improves test execution reliability and speed.