|
| 1 | +# API Test Module |
| 2 | + |
| 3 | +This module contains api tests that run against a Docker containerized version of the application. |
| 4 | +The tests make HTTP calls to verify the API endpoints are working correctly. |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +The `apiTest` module is a standalone Gradle project that: |
| 9 | +- Runs api tests against the application running in Docker containers |
| 10 | +- Automatically manages Docker containers (starts before tests, stops after) |
| 11 | +- Generates HTML and XML test reports |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +Before running the tests, ensure you have the following installed and configured: |
| 16 | + |
| 17 | +### Required Software |
| 18 | + |
| 19 | +1. **Java 21** (or higher) |
| 20 | + - Verify installation: `java -version` |
| 21 | + - Should show version 21 or higher |
| 22 | + |
| 23 | +2. **Docker Desktop** (or Docker Engine) |
| 24 | + - Verify installation: `docker --version` |
| 25 | + - Docker must be running (check with `docker ps`) |
| 26 | + - Docker Compose V2 must be available: `docker compose version` |
| 27 | + |
| 28 | +### System Requirements |
| 29 | + |
| 30 | +- At least 4GB of free RAM (Docker containers need memory) |
| 31 | +- Ports available: `8082` (application), `5432` (PostgreSQL), `9999` (WireMock) |
| 32 | +- Sufficient disk space for Docker images |
| 33 | + |
| 34 | +## Running Tests |
| 35 | + |
| 36 | +### From the Root Directory |
| 37 | + |
| 38 | +```bash |
| 39 | +# Navigate to the apiTest directory |
| 40 | +cd apiTest |
| 41 | + |
| 42 | +# Run all tests |
| 43 | +../gradlew test |
| 44 | + |
| 45 | +# Run tests with more verbose output |
| 46 | +../gradlew test --info |
| 47 | + |
| 48 | +# Run tests with debug output |
| 49 | +../gradlew test --debug |
| 50 | +``` |
| 51 | + |
| 52 | +### From the apiTest Directory |
| 53 | + |
| 54 | +```bash |
| 55 | +# If you're already in the apiTest directory |
| 56 | +../gradlew test |
| 57 | +``` |
| 58 | + |
| 59 | +### What Happens When You Run Tests |
| 60 | + |
| 61 | +1. **Builds the root project's bootJar** - The application JAR is built first |
| 62 | +2. **Builds Docker images** - Creates the application Docker image |
| 63 | +3. **Starts Docker containers**: |
| 64 | + - PostgreSQL database (port 5432) |
| 65 | + - Application server (port 8082) |
| 66 | + - WireMock server (port 9999) |
| 67 | +4. **Runs tests** - Executes all test classes |
| 68 | +5. **Stops and removes containers** - Cleanup after tests complete |
| 69 | + |
| 70 | +### Running Specific Tests |
| 71 | + |
| 72 | +```bash |
| 73 | +# Run a specific test class |
| 74 | +../gradlew test --tests "RootApiTest" |
| 75 | + |
| 76 | +# Run a specific test method |
| 77 | +../gradlew test --tests "RootApiTest.root_endpoint_should_be_ok" |
| 78 | +``` |
| 79 | + |
| 80 | +## Test Reports |
| 81 | + |
| 82 | +After running tests, you can view the results in several formats: |
| 83 | + |
| 84 | +### HTML Test Report (Recommended) |
| 85 | + |
| 86 | +**Location:** `apiTest/build/reports/tests/test/index.html` |
| 87 | + |
| 88 | +The HTML report includes: |
| 89 | +- Test summary (total, passed, failed, skipped) |
| 90 | +- Individual test results with execution times |
| 91 | +- Stack traces for failed tests |
| 92 | +- Package and class-level summaries |
| 93 | + |
| 94 | +### JUnit XML Reports |
| 95 | + |
| 96 | +**Location:** `apiTest/build/test-results/test/` |
| 97 | + |
| 98 | +These XML reports are useful for CI/CD integration. |
| 99 | + |
| 100 | +## Troubleshooting |
| 101 | + |
| 102 | +### Issue: "Could not start Gradle Test Executor 1: Failed to load JUnit Platform" |
| 103 | + |
| 104 | +**Solution:** This should be resolved with the current configuration. If you see this error: |
| 105 | +1. Clean the build: `../gradlew clean` |
| 106 | +2. Rebuild: `../gradlew build` |
| 107 | + |
| 108 | +### Issue: "no main manifest attribute, in /app/apiTest-0.0.999.jar" |
| 109 | + |
| 110 | +**Solution:** This means the Docker build context is wrong. Ensure: |
| 111 | +1. The `docker-compose.yml` has `context: ..` (builds from root directory) |
| 112 | +2. The root project's `bootJar` is built before Docker build |
| 113 | +3. Run: `../gradlew buildRootBootJar` manually if needed |
| 114 | + |
| 115 | +### Issue: Container exits with code 1 |
| 116 | + |
| 117 | +**Check application logs:** |
| 118 | +```bash |
| 119 | +# View app container logs |
| 120 | +docker logs apitest-app-1 |
| 121 | + |
| 122 | +# Or using docker-compose |
| 123 | +docker-compose -f docker-compose.yml logs app |
| 124 | + |
| 125 | +# View all container logs |
| 126 | +docker-compose -f docker-compose.yml logs |
| 127 | +``` |
| 128 | + |
| 129 | +**Common causes:** |
| 130 | +- Application configuration errors |
| 131 | +- Database connection issues |
| 132 | +- Missing environment variables |
| 133 | +- Port conflicts |
| 134 | + |
| 135 | +### Issue: Port already in use |
| 136 | + |
| 137 | +**Solution:** Stop any services using the required ports: |
| 138 | +```bash |
| 139 | +# Check what's using port 8082 |
| 140 | +lsof -i :8082 |
| 141 | + |
| 142 | +# Check what's using port 5432 |
| 143 | +lsof -i :5432 |
| 144 | + |
| 145 | +# Stop conflicting services or change ports in docker-compose.yml |
| 146 | +``` |
| 147 | + |
| 148 | +### Issue: Cannot connect to database |
| 149 | + |
| 150 | +**Solution:** |
| 151 | +- Ensure the database container is healthy: `docker ps` should show "Healthy" |
| 152 | +- Check database logs: `docker-compose -f docker-compose.yml logs db` |
| 153 | +- Verify connection string in `docker-compose.yml` matches database configuration |
| 154 | + |
| 155 | +## Manual Container Management |
| 156 | + |
| 157 | +If you need to manually manage containers: |
| 158 | + |
| 159 | +```bash |
| 160 | +# Start containers without running tests |
| 161 | +docker-compose -f docker-compose.yml up -d |
| 162 | + |
| 163 | +# Stop containers |
| 164 | +docker-compose -f docker-compose.yml down |
| 165 | + |
| 166 | +# View container logs |
| 167 | +docker-compose -f docker-compose.yml logs -f app |
| 168 | + |
| 169 | +# Check container status |
| 170 | +docker-compose -f docker-compose.yml ps |
| 171 | + |
| 172 | +# Rebuild containers |
| 173 | +docker-compose -f docker-compose.yml build --no-cache |
| 174 | +``` |
| 175 | + |
| 176 | +## Test Configuration |
| 177 | + |
| 178 | +### Environment Variables |
| 179 | + |
| 180 | +Tests use the following default configuration: |
| 181 | +- Application base URL: `http://localhost:8082` (can be overridden with `app.baseUrl` system property) |
| 182 | +- Database: PostgreSQL on port 5432 |
| 183 | +- WireMock: Port 9999 |
| 184 | + |
| 185 | +### Customizing Test Execution |
| 186 | + |
| 187 | +You can override the application URL: |
| 188 | +```bash |
| 189 | +gradle test -Dapp.baseUrl=http://localhost:8080 |
| 190 | +``` |
| 191 | + |
0 commit comments