{
    "slug": "integration_testing",
    "term": "Integration Testing",
    "category": "testing",
    "difficulty": "intermediate",
    "short": "Tests that verify multiple components work correctly together — often involving real databases, HTTP clients, or third-party services.",
    "long": "Integration tests verify that components interact correctly when combined — e.g., that a repository correctly persists and retrieves domain objects from a real database, or that an HTTP controller returns the expected response for a given request. They are slower and more expensive than unit tests but catch bugs that only emerge from component interactions. In PHP, integration tests typically use a test database, in-memory SQLite, or containers (via Docker or Laravel TestContainers). Follow the Test Pyramid: many unit tests, fewer integration tests, few E2E tests.",
    "aliases": [
        "integration tests",
        "component testing",
        "service integration test"
    ],
    "tags": [
        "general",
        "testing",
        "quality"
    ],
    "misconception": "Integration tests are just slow unit tests and should be minimised. Integration tests verify that components work together — they catch contract mismatches between layers that unit tests with mocks miss entirely. A test pyramid needs both, with unit tests forming the base.",
    "why_it_matters": "Integration tests verify that components work correctly together — they catch the mismatches between units that pass individually but fail when combined. A suite of only unit tests gives false confidence about real behaviour.",
    "common_mistakes": [
        "Using real external services (payment APIs, email providers) in integration tests — use sandbox environments or contract tests.",
        "Not isolating test data — tests that share database state interfere with each other and produce random failures.",
        "Making integration tests so broad they take minutes to run — they should be slower than unit tests but not by orders of magnitude.",
        "Not running integration tests in CI — they catch the bugs that matter most."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "unit_testing",
        "test_driven_development",
        "continuous_integration"
    ],
    "prerequisites": [
        "unit_testing",
        "test_environment_parity",
        "database_migrations"
    ],
    "refs": [
        "https://martinfowler.com/bliki/IntegrationTest.html"
    ],
    "bad_code": "// Mock that doesn't match real behaviour:\n$mockRepo = $this->createMock(UserRepository::class);\n$mockRepo->method('findByEmail')->willReturn($user); // Always returns a user\n// Real DB: findByEmail throws on DB connection failure\n// Integration test catches what the mock hides",
    "good_code": "class OrderApiTest extends TestCase {\n    use RefreshDatabase; // reset DB per test\n\n    public function test_place_order_stores_and_emails(): void {\n        Mail::fake();\n        $user = User::factory()->create();\n\n        $response = $this->actingAs($user)\n            ->postJson('/api/orders', ['sku' => 'WIDGET-1', 'qty' => 2]);\n\n        $response->assertStatus(201);\n        $this->assertDatabaseHas('orders', ['user_id' => $user->id]);\n        Mail::assertSent(OrderConfirmation::class);\n    }\n}",
    "example_note": "Integration tests hit real infrastructure (database, queue) — use a test database and fakes for external services.",
    "quick_fix": "Run integration tests against a real database in a Docker container in CI — use transactions with rollback to keep tests isolated and fast",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/integration_testing",
        "html_url": "https://codeclaritylab.com/glossary/integration_testing",
        "json_url": "https://codeclaritylab.com/glossary/integration_testing.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Integration Testing](https://codeclaritylab.com/glossary/integration_testing) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/integration_testing"
            }
        }
    }
}