# Apex unit test data and test case design

*Platform Development Lifecycle and Deployment Architect Study Guide · Building · Last updated October 26, 2026 · https://www.certifyforce.com/study-guide/platform-development-lifecycle-and-deployment-architect/building/apex-unit-test-data-and-test-case-design*

How Apex tests isolate and create their own data with factories, @testSetup and CSV static resources, and how to design positive, negative, permission-based and bulk test cases that prove behaviour instead of chasing a coverage number.

- Certification: Salesforce Platform Development Lifecycle and Deployment Architect
- Exam section: Building
- Study guide: https://www.certifyforce.com/study-guide/platform-development-lifecycle-and-deployment-architect

**Official exam objective:**

> Describe the appropriate approaches to building test data strategy and unit test to ensure successful code (positive, negative, permission-based, large data volume).

## What the exam expects

- Recommend how tests get their data: isolated, self-created records through a factory, `@testSetup` or a CSV static resource, not `SeeAllData=true`.
- Design positive, negative, permission-based and bulk test cases, and say what each proves.
- Separate what unit tests prove (bulk-safe logic near governor limits) from what load and volume tests in a Full sandbox prove (real volumes, skew, contention).
- Diagnose data-dependent and parallel-execution failures such as UNABLE_TO_LOCK_ROW.
- Keep tests focused, mock callouts and keep personal data out of exception messages.

Key terms: **test data isolation** (tests see only the data they create, plus setup and metadata objects), **test data factory** (an `@IsTest` class whose methods build records), **`@testSetup`** (a method that creates records once per class), **negative test** (proves invalid input or failure is handled), **`System.runAs`** (runs a block as a given user so that user's sharing and permissions apply).

## Key facts

| Fact | Detail | Source |
|---|---|---|
| Default data access | API 24.0+ tests see only their own data plus objects like User, Profile, RecordType | [Isolation of Test Data](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data_access.htm) |
| Triggers fired by tests | Under the same data restriction | [Isolation of Test Data](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data_access.htm) |
| Cleanup | Tests don't commit, so no deletes needed | [Testing Best Practices](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm) |
| `@testSetup` limits | One per class; not with `SeeAllData=true`; fatal error fails the class | [Using Test Setup Methods](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm) |
| Coverage rule | At least 75% of Apex; every trigger has some coverage | [Testing Best Practices](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm) |
| Bulk trigger tests | Use at least 20 records | [Testing Best Practices](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm) |
| `runAs` scope | Test methods only; ignores license limits; counts as DML | [Using the runAs Method](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm) |
| Parallel lock errors | Disable Parallel Apex Testing in Apex Test Execution options | [Testing Best Practices](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm) |

## Check yourself

- Why must a test that runs in every environment build its own records?
- When does a change made by one test method to `@testSetup` records affect the next method?
- Why should a negative test check the caught exception's type and message?
- Which records can an API 24.0+ test read without `SeeAllData`?

## Sources

- [Testing Best Practices](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm)
- [Isolation of Test Data from Organization Data in Unit Tests](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data_access.htm)
- [Using Test Setup Methods](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_using.htm)
- [Generate Data for Tests](https://trailhead.salesforce.com/content/learn/modules/unit-testing-on-the-lightning-platform/generate-data-for-tests)
- [Write Positive Tests](https://trailhead.salesforce.com/content/learn/modules/unit-testing-on-the-lightning-platform/positive-tests)
- [Write Negative Tests](https://trailhead.salesforce.com/content/learn/modules/unit-testing-on-the-lightning-platform/negative-tests)
- [Permission-Based Tests](https://trailhead.salesforce.com/content/learn/modules/unit-testing-on-the-lightning-platform/permission-based-tests)
- [Using the runAs Method](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_runas.htm)
- [Testing HTTP Callouts with HttpCalloutMock](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_http_testing_httpcalloutmock.htm)
- [Operational Excellence - DevOps Patterns](https://architect.salesforce.com/docs/architect/well-architected/guide/operational-excellence-devops-patterns.html)
- [Reliability](https://architect.salesforce.com/docs/architect/well-architected/guide/reliability.html)

## About the full unit

The "Core concepts", "Exam traps" and "Worked scenario" sections of this unit, its 10 linked practice questions, and unlimited timed mock exams are included with the Platform Development Lifecycle and Deployment Architect pack (USD $19): https://www.certifyforce.com/practice-exams
