Your masking tool runs on schedule, scrubs every object in the sandbox, and hands the admin a clean report. Meanwhile, a developer's Apex test class still contains "John.Smith@realcustomer.com" and a hardcoded UK mobile number from 2021, sitting in version control, deployed to every sandbox refresh since. Masking tools operate on data at rest in the database. They have no mechanism for scanning code, and Apex test classes are code, not records. That gap is wider than most Salesforce teams realize.
This isn't a theoretical risk. It's a pattern I see in nearly every org that has been live for more than three years. Someone needed a quick test fixture, copied a real contact record's values into a string literal, and moved on. The masking policy never saw it because masking policies target fields, not source files.
Why masking tools can't touch Apex test data
Data masking products, MaskEzee included, work by intercepting or rewriting field values during a sandbox refresh. They operate against the Salesforce database layer: Accounts, Contacts, Leads, custom objects, and their related child records. That's the right scope for 95% of PII exposure, because most real customer data lives in records.
Apex test classes don't live in records. They live in the Metadata API as source files, deployed via change sets, packages, or CI pipelines. A masking job that runs post-refresh never inspects a class body. It has no reason to; from its point of view, test classes are application logic, not data. The distinction is correct architecturally and useless from a compliance standpoint, because the PII is still sitting in a sandbox, just stored differently.
I'd argue this is the single most overlooked gap in sandbox data governance programs. Teams spend months tuning masking rules for fifty-plus fields and never once run a search across their Apex codebase for an @ symbol followed by a real domain.
Where hardcoded PII hides in test classes
It shows up in a handful of predictable places. Recognizing the pattern makes it much faster to audit.
- Copy-pasted test fixtures: A developer needed a valid-looking email and lifted one from a UAT export instead of generating a fake one.
- Static resource CSVs: Bulk test data loaded via Test.loadData(), often exported from a real org years ago and never scrubbed.
- Mock callout responses: JSON fixtures simulating an external API response that include real names, addresses, or phone numbers from an actual integration test.
- Debug.LogMessage strings: Occasionally someone pastes an actual customer record into a debug statement for "troubleshooting" and forgets to remove it before committing.
- Hardcoded assertion data: Assert statements comparing against a specific real phone number or national ID format pulled from production for a bug reproduction.
None of these show up in a data model review. They show up only if someone greps the codebase, and most teams never think to.
The compliance gap: DSARs and test fixtures
Under GDPR, a Data Subject Access Request covers personal data "processed" by the controller, which includes copies stored anywhere in your systems, not just production. If a data subject's email address sits hardcoded in an Apex class deployed across five sandboxes, that's five additional locations where their data exists outside of any retention or erasure control.
The practical problem is discoverability. Your masking audit trail will correctly show that Contact and Lead PII was replaced on every refresh. It says nothing about whether a real person's details are embedded in a static resource that's been deployed, unchanged, since before your masking policy existed. An auditor who asks "where else does this person's data exist in non-production systems" is asking a question your masking logs cannot answer.
This matters most for right-to-erasure requests. You can process an erasure against your production database and your masked sandboxes with confidence. You cannot credibly claim completeness if nobody has checked whether that same individual's name or email is sitting in a test class comment or a hardcoded assertion three repos deep.
Static resources and seed data scripts
Beyond individual test classes, larger orgs often maintain seed data scripts, bulk CSVs loaded as static resources to populate sandboxes with realistic volume for performance testing. These are frequently exports of actual production data taken once, years ago, with a note in the commit message saying "anonymize before reuse" that nobody acted on.
These files are worse than individual hardcoded strings because of volume. A single static resource can contain thousands of rows of real names, emails, and addresses, loaded fresh into every new scratch org or sandbox that references it. One client we worked with found a 40,000-row CSV in a static resource folder, untouched since 2019, used by a regression test suite that ran nightly, loading real customer PII into a sandbox every single night for five years.
The fix isn't complicated once you find it. The finding is the hard part, because static resources rarely get the same scrutiny as object-level data during a compliance review.
Fixing it: patterns for synthetic test data
The solution is a hard separation between test logic and test data provenance. A few concrete practices close most of the gap:
| Practice | Why it works |
|---|---|
| Use a faker library (System.Faker patterns or a custom utility class) to generate names, emails, and phone numbers at test runtime | No real value is ever stored in source control, because nothing is hardcoded |
| Ban literal email domains outside @test.invalid or @example.com via a static code analysis rule | Catches the copy-paste pattern before it merges |
| Audit static resources quarterly with a simple regex scan for email patterns and phone formats | Finds bulk seed data that individual code review misses |
| Require test data factories (a TestDataFactory.cls pattern) instead of ad hoc fixture creation | Centralizes data generation so one fix propagates everywhere |
None of this replaces your sandbox masking policy. It complements it. Masking handles the database; these practices handle the codebase. Skip either one and you have a partial compliance posture dressed up as a complete one.
Building a review process into CI
The most durable fix isn't a one-time cleanup, it's a gate. Add a static analysis step to your CI pipeline that flags common PII patterns, real-looking email domains, phone number formats matching actual country codes outside your test range, and known static resource file types, before a pull request can merge.
This doesn't need to be sophisticated. A regex scan for patterns like @gmail.com, @yahoo.com, or any domain that isn't on an approved test-domain allowlist catches the overwhelming majority of cases. Pair it with a quarterly manual audit of static resources larger than a few hundred rows, and you've covered both the slow drip of individual hardcoded values and the occasional bulk dump.
Admins and architects who own the sandbox masking program should treat this as part of the same mandate, even though it lives in a different tool. A masking vendor can tell you your Contact records are clean. Nobody but your own engineering team can tell you your test classes are too. Put both on the same compliance checklist, or the gap just moves from the database to the repo and waits there until an auditor or a DSAR finds it first.
Frequently Asked Questions
Can sandbox masking tools scan Apex code for hardcoded PII?
No. Masking tools operate on database records during a refresh, replacing field values in objects like Contact, Lead, and custom objects. Apex classes are metadata deployed through the Metadata API, not records in the database, so a masking job never inspects them. Finding hardcoded PII in code requires a separate static analysis or manual code review process.
Where is hardcoded PII most likely to appear in a Salesforce org?
The most common locations are Apex test classes using copy-pasted fixture data, static resource CSV files used for bulk seed data, mock callout response JSON, and hardcoded assertion values used to reproduce a specific bug. Debug log statements that paste in real record values are also a frequent source.
Does GDPR consider test data in sandboxes a compliance risk?
Yes. GDPR applies to personal data wherever it is processed, including copies held in non-production environments. If a real data subject's name or email is hardcoded into a test class or static resource, that counts as personal data processing and falls within scope for data subject access and erasure requests.
How do you find hardcoded PII in an existing Apex codebase?
A regex scan across the repository for email patterns, phone number formats, and known real domains (gmail.com, outlook.com, and similar) will surface most cases quickly. Static resources should be checked separately since they often contain large CSV files that a simple code search in an IDE might skip over. Running this scan once and then adding it as a CI gate prevents the problem from recurring.
What's the best way to generate test data without using real PII?
Build a centralized test data factory class that generates synthetic names, emails, and phone numbers at runtime using a faker-style pattern. Restrict all email domains used in tests to an allowlist like example.com or test.invalid, and enforce this through a code review rule or static analysis check. This keeps real customer data out of source control entirely rather than relying on developers to remember to anonymize it manually.