Balancing Unit and Integration Tests in Apex: A Practical Strategy

Balance unit and integration Apex tests—use mocks to isolate logic, reserve integration tests for external systems, and optimize CI pipelines for quick feedback.

## Why balancing matters

Every Salesforce org has limited CI time, fragile integration points, and complex platform behaviors. Treating all tests the same leads to slow pipelines and flaky failures. A deliberate balance between unit and integration tests keeps feedback fast, coverage meaningful, and releases safe.

## A simple testing pyramid for Apex

### Unit tests — fast, isolated, dominant

Focus the majority of test effort on unit tests that validate business logic in isolation. Best practices:

- Use @IsTest(SeeAllData=false) to prevent dependency on org data.

- Inject collaborators via interfaces or simple factories so logic can be mocked.

- Use Test.createStub and HttpCalloutMock for callouts or external interactions.

- Wrap asynchronous and bulk behavior using Test.startTest and Test.stopTest to verify limits and queueable behavior.

Aim for 70–85% of test count and the fastest feedback loop in CI.

### Integration tests — targeted, realistic, fewer

Integration tests exercise real integrations and platform contracts: external APIs, Named Credentials, platform events, and managed package interfaces. Keep them limited and intentional:

- Reserve integration tests for scenarios where behavior depends on the external system.

- Use dedicated test endpoints or sandbox accounts to avoid modifying production state.

- Record canonical responses when possible and replay with mocks in most runs; run a small set of live integration tests on a scheduled basis.

Target 15–30% of tests, focused on end-to-end correctness rather than exhaustive permutations.

## Practical CI strategy

Split test execution into stages:

1. Pre-commit/PR: run the fast unit test suite only. Fail fast on logic regressions.

2. Merge: run an expanded suite including smoke integration tests that validate critical integrations.

3. Nightly: run full integration and long-running scenarios, including any tests that rely on external systems.

This separation preserves developer productivity while keeping integration confidence high.

## Stability checklist for integration tests

- Use stable test accounts and sandbox endpoints.

- Limit data creation to clearly scoped records and clean up after tests when possible.

- Time-box retries and use idempotent test design to handle transient external failures.

- Record and audit external interactions so you can replay them when the external system is unavailable.

## Measuring success

Track test run time, flakiness rate, and mean time to repair failing tests. Aim to reduce flakiness by isolating logic and moving unstable scenarios into scheduled integration runs.

Conclusion / Call to action

Balancing unit and integration tests in Apex is a practical lever for better quality and faster CI. Start by shifting the bulk of tests to fast, isolated unit tests, and adopt a staged CI strategy for integration coverage. If you want to accelerate creating both unit and integration-ready test scaffolding, try Test Class Generator to generate reusable, maintainable Apex tests that follow these best practices.