Tutorial on using Apex AI to standardize Apex test class generation across Salesforce teams with consistent prompts, conventions, and review steps.
Consistency is one of the hardest parts of maintaining a healthy Salesforce codebase-especially when multiple engineers write tests in different styles, with different data factories, and varying coverage quality. This tutorial shows how to use Apex AI as a shared, repeatable workflow so your team generates Apex test classes that follow the same patterns, naming conventions, and quality bar.
## What you’ll achieve in this tutorial
By the end, you’ll have a repeatable way to:
- Generate Apex test classes with consistent structure and naming
- Enforce team conventions (data factories, @testSetup usage, asserts)
- Reduce review cycles caused by test style and missing edge cases
- Keep test output aligned with your org’s architecture
## Prerequisites
Before you start, make sure you have:
- Access to Apex AI in your environment
- The Apex class or trigger you want to test (existing or newly created)
- Your team’s agreed conventions (or a baseline you want to standardize on), such as:
- Test class naming: `ClassNameTest` or `Test_ClassName`
- Preferred patterns: data factory methods, `@testSetup`, and explicit assertions
- Expected framework usage (if any), e.g., fflib-style factories/selectors
## Step 1: Define your “team default” test requirements
To get consistent output, start by standardizing what “good” means for your team. Document a short checklist that Apex AI should follow, such as:
- Always include at least one positive and one negative test
- Use `@testSetup` for shared data
- Avoid `SeeAllData=true`
- Use meaningful assertions (not just coverage)
- Cover bulk behavior (lists > 1) when applicable
This becomes the rubric you apply every time you use the application.
## Step 2: Use Apex AI with a structured prompt template
When generating a test class, provide Apex AI with the Apex source plus your standardized instructions. A reliable format is:
### Recommended prompt template
- **Target**: “Generate tests for `MyService.cls`.”
- **Constraints**: “No SeeAllData. Use @testSetup. Use `TestDataFactory` methods.”
- **Coverage goals**: “Cover null/empty inputs, exceptions, bulk behavior.”
- **Assertions**: “Assert expected field values, DML outcomes, and error messages.”
Using the same prompt skeleton across the team dramatically reduces variation.
## Step 3: Review for org-specific alignment (fast, not optional)
AI-generated tests should still be reviewed-quickly and consistently. Have engineers validate:
- The test data aligns with required fields, validation rules, and record types
- The test uses the correct factory/builders your org expects
- Assertions verify behavior (outputs and side effects), not just lines executed
- Edge cases match your domain rules (e.g., status transitions, entitlement logic)
If something is off, re-run Apex AI with a tighter constraint (e.g., “Use RecordType ‘Customer_Support’ and populate required fields: X, Y, Z.”).
## Step 4: Make it repeatable in your team workflow
To standardize results at scale, treat Apex AI like a shared internal tool:
- Add a short “Apex AI Test Generation” section to your team’s engineering handbook
- Include the prompt template in your repo docs (so everyone uses the same baseline)
- Encourage reviewers to request “regenerate with constraints” instead of rewriting tests by hand
- Track common fixes (missing required fields, incorrect factory usage) and fold them into the default prompt
## Conclusion / Call-to-action
Apex AI is most powerful when it’s used as a consistent, team-wide workflow-not a one-off helper. Standardize your prompts, enforce your test checklist, and you’ll ship reliable Apex tests faster with fewer review cycles. Ready to operationalize it? Start by applying the prompt template above to one service class and make it your team’s default.