Skip to content
Back to skills

Integration Testing

ASecurity

Use when the user asks about integration tests, API tests against a real database, Testcontainers, WireMock, component tests of a service or testing repositories and queues. Produces integration test setup with isolated dependencies. Not for unit tests with mocks (use quality-unit-testing) or Pact contracts (use quality-contract-testing).

  • 46 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added November 27, 2026
ai-agentstypescriptpythonjavasqlnodespringdockerawsgcptesting

Works with

  • cursor
  • cli
  • api

Security analysis

A100/100

Pro scans all 12 files and shows the line behind each finding

Scanned November 27, 2026

npx -y skills add j4flmao/agent-skills --skill integration-testing --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Integration Testing?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Integration Testing
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/j4flmao-integration-testing/badge)](https://www.skillsdirectory.com/skills/j4flmao-integration-testing)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: quality-integration-testing
description: >
  Use when the user asks about integration tests, API tests against a real database, Testcontainers, WireMock, component tests of a service or testing repositories and queues. Produces integration test
  setup with isolated dependencies. Not for unit tests with mocks (use quality-unit-testing) or Pact contracts (use quality-contract-testing).
version: "2.0.0"
author: "j4flmao"
license: "MIT"
last_verified: "2026-11-17"
compatibility:
  claude-code: true
  cursor: true
  codex: true
  windsurf: true
tags: [quality, integration-testing, phase-3]
---

# Integration Testing

## Purpose
Design and implement integration tests that verify components work together: API integration, database integration, external service mocking, and containerized infrastructure for realistic environments. This skill covers test architecture, container lifecycle management, data strategies, and CI integration.

## Agent Protocol

### Trigger
User mentions integration testing, component testing, API testing, database testing, TestContainers, WireMock, service-level testing, or inter-component verification.

### Input Context
- Components/services under test and their interaction patterns
- Infrastructure dependencies (databases, message queues, caches)
- External service integrations
- Existing test suite and coverage levels
- CI environment capabilities (Docker availability)

### Output Artifact
Integration test suite with containerized infrastructure, service virtualization, and CI configuration.

### Response Format
Structured integration test files with:
1. Container lifecycle management (start, wait, cleanup)
2. Test data setup and cleanup strategies
3. External service stubs/mocks
4. CI pipeline configuration with parallel execution

### Completion Criteria
- All component interactions tested with real (containerized) infrastructure
- External services simulated with WireMock or similar
- Database state management strategy defined (truncation, rollback, or fresh containers)
- CI integration configured with Docker-in-Docker and parallel execution
- Error paths tested (timeouts, failures, validation errors)

## Workflow

1. **Identify interaction points**: Map all component boundaries (database, API, message queue, external services)
2. **Select integration strategy**: For each dependency, decide real (containerized) vs simulated (WireMock). Use real for your own infrastructure, simulated for external services
3. **Configure containers**: Set up TestContainers with appropriate wait strategies, connection strings, and lifecycle hooks
4. **Design test data**: Create factory functions for test entities. Plan data setup and cleanup strategies
5. **Implement service stubs**: Configure WireMock/MockServer for external services based on contracts
6. **Write integration tests**: Structure with beforeAll (containers), beforeEach (data setup), test (AAA), afterEach (cleanup), afterAll (container stop)
7. **Test error paths**: Include timeouts, network failures, validation errors, auth failures, and resource exhaustion
8. **Optimize execution**: Parallelize container startup, share containers across suites, configure appropriate timeouts
9. **Integrate CI**: Configure Docker-in-Docker, pre-pull images, set generous timeouts, parallelize test execution
10. **Monitor flakiness**: Track flaky tests, investigate infrastructure-related failures, quarantine non-deterministic tests

## Architecture / Decision Trees

### Dependency Strategy Decision Tree

```
Is this dependency your own service/infrastructure?
├── YES → Use real (containerized) for testing
│   ├── Database → TestContainers (PostgreSQL, MySQL, etc.)
│   ├── Message queue → TestContainers (Kafka, RabbitMQ)
│   ├── Cache → TestContainers (Redis)
│   └── Object storage → TestContainers (MinIO)
└── NO → Is it a third-party external service?
    ├── YES → Simulate with WireMock (contract-based stubs)
    └── NO → Is it an infrastructure provider (AWS, GCP)?
        ├── YES → Use LocalStack or similar emulator
        └── NO → Is it a library with side effects?
            ├── YES → Mock at adapter boundary
            └── NO → Real implementation
```

### Container Lifecycle Decision Tree

```
How many test suites share this service?
├── One suite → Start/stop per suite (beforeAll/afterAll)
└── Multiple suites → Can they share?
    ├── YES → Singleton container pattern (shared across suites)
    └── NO → Start/stop per suite

Is state isolation critical?
├── YES → Fresh container per test class
└── NO → Data cleanup between tests (truncation)

Does CI support Docker?
├── YES → Testcontainers against the runner's Docker daemon (GitHub-hosted Linux runners have Docker)
└── NO → Embedded services or mocked infrastructure
```

## Common Pitfalls

1. **In-memory database substitutes**: H2/SQLite are not PostgreSQL/MySQL — different SQL dialect, constraints, and transaction behavior. Always use the real database in a container
2. **No data cleanup**: Tests that leave data behind cause non-deterministic failures in subsequent tests. Implement truncation, delete-by-run-id, or fresh containers
3. **Hardcoded connection parameters**: Hardcoded ports, hosts, or credentials prevent parallel execution. Use dynamic ports and environment-aware configuration
4. **Missing wait strategies**: Tests that access containers before they're ready fail intermittently. Always use proper wait strategies (log message, HTTP health check, port listening)
5. **Over-mocking external services**: Stubbing every possible response creates brittle tests that don't reflect real behavior. Use contracts to define stubs
6. **Ignoring container startup time**: Not setting generous beforeAll timeouts causes test failures in CI. Set timeout to 120s+ for container startup
7. **Testing with production data**: Risk of data corruption, compliance violations, and non-reproducible failures
8. **Shared state across parallel tests**: Multiple workers accessing the same database cause conflicts. Use isolated databases or schemas per worker
9. **No error path testing**: Only testing happy paths misses timeouts, retries, circuit breakers, and validation failures
10. **Resource leaks**: Not stopping containers, closing connections, or cleaning up temp files after tests

## Best Practices

1. Always use real (containerized) databases, not in-memory substitutes
2. Use TestContainers for container lifecycle management with proper wait strategies
3. Mock external services at the HTTP boundary using WireMock with contract-based stubs
4. Clean database state between tests (truncation is preferred for most cases)
5. Set generous timeouts for container startup (120s+ in CI)
6. Start containers in parallel using Promise.all for multi-service tests
7. Use environment-aware configuration (local vs CI) for connection strings
8. Test error paths as thoroughly as happy paths
9. Reuse containers across test suites for speed, but isolate state per test
10. Monitor flakiness and fix non-deterministic tests immediately

## Compared With

| Aspect | Integration Testing | Unit Testing | E2E Testing |
|--------|-------------------|-------------|-------------|
| Scope | Component interactions | Single module | Full user workflow |
| Dependencies | Real (containerized) | Mocked | Real (full system) |
| Speed | Seconds to minutes | Milliseconds | Minutes |
| Confidence | High (real infra) | Low (isolated) | Very High |
| Debugging | Medium | Easy | Hard |
| Infrastructure | Docker/containers | None | Full environment |
| When | After unit tests | Every commit | Pre-release |

## Performance Considerations

- Container startup: 30-60s per container. Start in parallel, reuse across suites
- Database migration: 5-30s per suite. Run once per suite, not per test
- Data cleanup: Truncation (100ms) vs fresh container (30s). Choose based on isolation needs
- Test execution: Aim for < 15 minutes per suite. Use parallel workers, each with its own database
- CI resources: Docker-in-Docker requires privileged mode or dedicated runners. Plan resource allocation
- Network: Container-to-container communication is fast (< 1ms). External service calls add latency

## Integration Test Examples

### Python + TestContainers — Database Integration Test
```python
import pytest
from testcontainers.postgres import PostgresContainer
from sqlalchemy import create_engine, text

@pytest.fixture(scope="module")
def postgres_container():
    with PostgresContainer("postgres:16-alpine") as pg:
        engine = create_engine(pg.get_connection_url())
        with engine.begin() as conn:
            conn.execute(text("""
                CREATE TABLE users (
                    id SERIAL PRIMARY KEY,
                    email VARCHAR(255) UNIQUE NOT NULL,
                    name VARCHAR(100) NOT NULL
                )
            """))
        yield pg

@pytest.fixture
def db(postgres_container):
    engine = create_engine(postgres_container.get_connection_url())
    yield engine
    with engine.begin() as conn:
        conn.execute(text("TRUNCATE TABLE users RESTART IDENTITY CASCADE"))

class TestUserRepository:
    def test_create_user(self, db):
        with db.begin() as conn:
            result = conn.execute(
                text("INSERT INTO users (email, name) VALUES (:email, :name) RETURNING id"),
                {"email": "test@example.com", "name": "Test User"},
            )
            user_id = result.scalar()
        assert user_id is not None

    def test_find_user_by_email(self, db):
        with db.begin() as conn:
            conn.execute(
                text("INSERT INTO users (email, name) VALUES (:email, :name)"),
                {"email": "find@example.com", "name": "Find Me"},
            )
        with db.begin() as conn:
            result = conn.execute(
                text("SELECT * FROM users WHERE email = :email"),
                {"email": "find@example.com"},
            )
            user = result.fetchone()
        assert user is not None
        assert user.name == "Find Me"

    def test_email_uniqueness(self, db):
        with db.begin() as conn:
            conn.execute(
                text("INSERT INTO users (email, name) VALUES (:email, :name)"),
                {"email": "dup@example.com", "name": "First"},
            )
        with pytest.raises(Exception):
            with db.begin() as conn:
                conn.execute(
                    text("INSERT INTO users (email, name) VALUES (:email, :name)"),
                    {"email": "dup@example.com", "name": "Second"},
                )
```

### Java + TestContainers + WireMock — Service Integration Test
```java
@SpringBootTest
@Testcontainers
class OrderServiceIntegrationTest {

    @Container
    static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine");

    @Container
    static WireMockContainer wiremock = new WireMockContainer("wiremock/wiremock:3.5.4"); // org.wiremock.integrations.testcontainers

    @DynamicPropertySource
    static void configureProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", postgres::getJdbcUrl);
        registry.add("spring.datasource.username", postgres::getUsername);
        registry.add("spring.datasource.password", postgres::getPassword);
        registry.add("payment.service.url", wiremock::getBaseUrl);
    }

    @Autowired
    private OrderService orderService;

    @BeforeEach
    void pointWireMockClientAtContainer() {
        // static stubFor(...) talks to localhost:8080 unless configured
        WireMock.configureFor(wiremock.getHost(), wiremock.getPort());
    }

    @Test
    void shouldCreateOrderWhenPaymentSucceeds() {
        stubFor(post(urlEqualTo("/api/payments"))
            .willReturn(aResponse()
                .withStatus(200)
                .withHeader("Content-Type", "application/json")
                .withBody("""
                    {"id": "pay_123", "status": "confirmed"}
                    """)));

        Order order = orderService.placeOrder(customerId, cartItems);
        assertThat(order.getStatus()).isEqualTo("CONFIRMED");
        assertThat(order.getPaymentId()).isEqualTo("pay_123");
    }
}
```

### TypeScript + TestContainers — Message Queue Integration Test
```typescript
import { KafkaContainer, type StartedKafkaContainer } from "@testcontainers/kafka";
import { Kafka, type Consumer } from "kafkajs";
import { OrderEventPublisher } from "../src/order-event-publisher";

describe("Order Event Publisher", () => {
  let container: StartedKafkaContainer;
  let kafka: Kafka;
  let consumer: Consumer;

  beforeAll(async () => {
    container = await new KafkaContainer("confluentinc/cp-kafka:7.6.0").start();
    kafka = new Kafka({
      clientId: "test",
      brokers: [`${container.getHost()}:${container.getMappedPort(9093)}`],
    });
  }, 120_000);

  afterAll(async () => {
    await consumer?.disconnect();
    await container.stop();
  });

  it("publishes an order-created event", async () => {
    consumer = kafka.consumer({ groupId: `test-${Date.now()}` });
    await consumer.connect();
    await consumer.subscribe({ topic: "order-events", fromBeginning: true });

    const received = new Promise<Record<string, unknown>>((resolve) => {
      void consumer.run({
        eachMessage: async ({ message }) => resolve(JSON.parse(message.value!.toString())),
      });
    });

    const publisher = new OrderEventPublisher(kafka.producer());
    await publisher.publishOrderCreated({ orderId: "ord_123" });

    // Wait for the actual message instead of sleeping a fixed time
    const event = await Promise.race([
      received,
      new Promise<never>((_, reject) => setTimeout(() => reject(new Error("No order-created event within 10s")), 10_000)),
    ]);
    expect(event.orderId).toBe("ord_123");
  });
});
```

## CI Integration for Integration Tests

```yaml
name: Integration Tests
on: pull_request
jobs:
  integration:
    runs-on: ubuntu-latest
    timeout-minutes: 30
    strategy:
      matrix:
        shard: [1, 2]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: 24
      - run: npm ci
      - name: Pre-pull container images
        run: |
          docker pull postgres:16-alpine
          docker pull confluentinc/cp-kafka:7.6.0
          docker pull wiremock/wiremock:3.5.4
      - name: Run integration tests
        run: npx vitest run --config vitest.integration.config.ts --shard=${{ matrix.shard }}/2
        env:
          CI: true
      - uses: actions/upload-artifact@v4
        if: failure()
        with:
          name: integration-logs-${{ matrix.shard }}
          path: logs/
```

## Integration Testing Anti-Patterns

### Anti-Pattern: In-Memory Database Substitutes
Using H2 for PostgreSQL or SQLite for MySQL. In-memory substitutes have different SQL dialects, constraint behaviors, and transaction semantics. Tests pass with H2 but fail with PostgreSQL in production. Always use the real database in a container.

### Anti-Pattern: No Wait Strategy
Accessing containers before they're ready produces non-deterministic failures. Never use fixed `Thread.sleep()`. Use predicate-based wait strategies: wait for log message ("database system is ready to accept connections"), HTTP health check, or port listening.

### Anti-Pattern: Shared Database State
Tests that leave data behind cause failures in subsequent tests. Use truncation between tests (`TRUNCATE ... CASCADE`). For fully isolated tests, use a fresh container per test class. Never rely on test ordering.

### Anti-Pattern: Over-Mocking External Services
Mocking at the HTTP client level (axios mocks, fetch mocks) instead of the network level. WireMock operates at the HTTP protocol level and validates real request/response contracts. HTTP client mocks are brittle and miss protocol-level issues.

### Anti-Pattern: No Error Path Tests
Testing only the happy path (successful database write, successful API response) misses the majority of integration issues. Every happy path must have a corresponding error path: network timeout, HTTP 500, database constraint violation, authentication failure.

### Anti-Pattern: Hardcoded Connection Parameters
Hardcoded ports, hosts, or credentials prevent parallel test execution. Use `withExposedPorts` for dynamic port mapping. Use `@DynamicPropertySource` (Spring) or environment variables to inject connection parameters.

## Integration Testing Maturity Model

| Level | Characteristics | Practices |
|---|---|---|
| 1: Initial | No integration tests | Unit tests only or manual integration testing, no containerized infrastructure |
| 2: Defined | Basic integration tests | TestContainers for database, basic WireMock stubs, sequential execution, some error path testing |
| 3: Managed | Structured integration suite | Containerized databases + message queues + caches, WireMock for all external services, parallel execution, CI integration with pre-pulled images |
| 4: Measured | Comprehensive integration coverage | Error path parity with happy path, migration testing (forward + rollback), performance-sensitive assertions, < 5% flakiness rate |
| 5: Optimized | Integration as release gates | All integration paths covered, chaos-resilient tests (network partition, resource exhaustion), automatic container layer caching, self-healing connection handling |

## Performance Considerations

- Container startup: 30-60s per container. Start in parallel for multi-service tests.
- Database migration: 5-30s per suite. Run once per suite (beforeAll), not per test.
- Data cleanup: truncation (50-200ms) vs fresh container (30s). Choose based on isolation needs.
- Test execution: target < 15 minutes per suite. Parallel workers each get isolated database.
- CI resources: Docker-in-Docker requires privileged mode or dedicated runners. Use `TESTCONTAINERS_RYUK_DISABLED: true` in CI.
- Container image caching: pre-pull all images in CI to avoid network timeouts. Use `docker pull` in a setup step.
- WireMock startup: < 3s per instance. Reuse across tests within a suite.

## Rules
1. Every integration test must use real (containerized) databases — no H2, SQLite, or in-memory substitutes
2. External services must be simulated using WireMock or MockServer, never mocked at the HTTP client level
3. Database state must be cleaned between tests. Use truncation, delete-by-run-id, or rollback — never rely on test ordering
4. Container waiting must use predicate-based wait strategies (log messages, health checks) — never fixed sleep
5. beforeAll timeouts must be at least 120 seconds for container startup in CI environments
6. Integration tests must not share state across test suites — use isolated databases, topics, or containers
7. Every happy path test must have a corresponding error path test (timeout, 500, validation failure)
8. Test data must use factories with generated values — never use production data or hardcoded IDs
9. Containers must be stopped in afterAll — resource leaks are not acceptable
10. External service mocks must match at least the contract (URL, method, headers, response shape)
11. Integration tests must not access production endpoints or use production credentials
12. Container images must be pre-pulled in CI to prevent network timeouts during test execution
13. Integration test configuration must be environment-aware (local/CI/staging) with no hardcoded parameters
14. Flaky integration tests (non-deterministic) must be quarantined within 24 hours
15. Migration tests must verify both forward and rollback scenarios
16. Performance-sensitive integration tests must include timing assertions with 2x safety margins
17. Parallel database isolation: each worker gets a separate database schema or container
18. WireMock stubs must be verified (at least one request matched) in afterEach

## References
- references/api-testing.md — API Integration Testing Patterns
- references/containerized-testing.md — Containerized Testing
- references/contract-driven.md — Contract-Driven Integration Testing
- references/database-testing.md — Database Integration Testing
- references/integration-testing-advanced.md — Integration Testing Advanced Topics
- references/integration-testing-architecture.md — Integration Testing Architecture and System Design
- references/integration-testing-fundamentals.md — Integration Testing Fundamentals
- references/integration-testing-strategy.md — Integration Testing Strategy and Workflow Patterns
- references/messaging-testing.md — Message Queue Integration Testing
- references/test-containers.md — TestContainers Patterns

### Shared References
Additional reference material for this skill, stored outside its own `references/` folder.

- `../../backend/universal/testing/references/integration-test-patterns.md` — Integration Test Patterns
- `../../backend/universal/testing/references/mocking-fakes.md` — Mocking and Test Doubles
- `../../backend/universal/testing/references/mocking-strategies.md` — Mocking Strategies
- `../../backend/universal/testing/references/test-naming-coverage.md` — Test Naming and Coverage
- `../../backend/universal/testing/references/test-pyramid.md` — Test Pyramid
- `../../backend/universal/testing/references/testing-contract.md` — Contract Testing
- `../../backend/universal/testing/references/testing-integration.md` — Integration Testing
- `../../backend/universal/testing/references/testing-property-based.md` — Property-Based Testing

## Handoff
After integration testing, hand off to:
- `quality-e2e-testing` — for end-to-end validation of complete workflows
- `quality-contract-testing` — for formal contract verification between services
- `quality-regression-testing` — for regression suite updates
- `quality-smoke-testing` — for BVT smoke test inclusion
## Architecture Decision Trees

### Integration Test Scope
| Decision Point | Option A | Option B | Decision Criteria |
|---|---|---|---|
| Dependency type | Internal microservice → Real instance | External API → Mock/stub | Control over service, cost, reliability |
| Database strategy | TestContainers (real DB in container) | In-memory H2/SQLite | Query compatibility, SQL dialect features |
| Message queue | Real broker (TestContainers Kafka) | Mock producer/consumer | Message ordering needs, throughput testing |
| Network complexity | WireMock for HTTP stubs | TestContainer for full service | Need for realistic network behavior |

### Test Granularity
- Single service handling → Test controller + service + repository with real DB
- Two-service interaction → Test REST/gRPC calls between them
- Multi-service workflow → Test orchestration through message queue
- External dependency → Test with WireMock + verify contract

Files in this skill

  • SKILL.md22.5 KB
  • references/api-testing.md1018 B
  • references/containerized-testing.md16.8 KB
  • references/contract-driven.md10.3 KB
  • references/database-testing.md8.8 KB
  • references/integration-testing-advanced.md6.3 KB
  • references/integration-testing-architecture.md12.5 KB
  • references/integration-testing-fundamentals.md8.2 KB
  • references/integration-testing-strategy.md14.2 KB
  • references/messaging-testing.md8.9 KB
  • references/test-containers.md1.2 KB
  • references/testcontainers-deep-dive.md2.6 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.