In Salesforce development, ensuring the quality and functionality of your code is essential. Testing and debugging help catch issues early, while deployment ensures that your work moves smoothly between environments.
A test class is an Apex class annotated with @isTest and contains test methods to verify code behavior.
The following is a simple example of a test class for an Account trigger:
Trigger to Test:
trigger AccountTrigger on Account (before insert) {
for (Account acc : Trigger.new) {
acc.Description = 'Auto-generated Description';
}
}
Test Class:
@isTest
public class AccountTest {
@isTest
static void testAccountTrigger() {
// Create a new Account record
Account acc = new Account(Name = 'Test Account');
insert acc;
// Validate the trigger logic
Account insertedAcc = [SELECT Description FROM Account WHERE Id = :acc.Id];
System.assertEquals('Auto-generated Description', insertedAcc.Description);
}
}
Setup Test Data:
Account record).Invoke the Code:
insert, update) that trigger your code.Assert Results:
Use System.assert methods to verify that the results match expectations.
Example:
System.assertEquals(expected, actual);
Test.loadData() to populate data.Debugging helps identify and fix issues in your code during development.
System.debug():
Outputs logs for analysis.
Example:
System.debug('The value of acc.Description is: ' + acc.Description);
Developer Console:
Debug Logs:
Add Meaningful Debug Statements:
Provide context in your debug logs.
Example:
System.debug('Processing Account record with ID: ' + acc.Id);
Filter Debug Logs:
Leverage Developer Console Features:
After developing and testing in a sandbox or developer environment, you need to deploy your changes to production or other sandboxes.
What is Salesforce DX?
Key Features:
Deployment Workflow with Salesforce DX:
Example Deployment Command:
sfdx force:source:deploy -p path/to/metadata
| Feature | Change Sets | Salesforce DX |
|---|---|---|
| Ease of Use | Simple, GUI-based | Requires CLI and technical skills |
| Complexity | Limited to supported metadata | Handles complex deployments |
| CI/CD Support | No | Yes |
| Speed | Manual process | Automated workflows |
@TestSetup is a special annotation in Apex testing that creates reusable test data for multiple test methods within the same test class.@isTest
public class AccountTest {
@TestSetup
static void setupTestData() {
Account acc = new Account(Name = 'Test Account');
insert acc;
}
@isTest
static void testAccountTrigger() {
Account insertedAcc = [SELECT Description FROM Account WHERE Name = 'Test Account'];
System.assertEquals('Auto-generated Description', insertedAcc.Description);
}
}
"Why should you use @TestSetup in a test class?"
@isTest
public class AsyncTest {
@isTest
static void testFutureMethod() {
Test.startTest();
FutureClass.someFutureMethod();
Test.stopTest();
System.assertEquals(1, [SELECT COUNT() FROM Async_Result__c]);
}
}
"What is the purpose of Test.startTest() and Test.stopTest() in a test class?"
Test.startTest() resets governor limits and provides a fresh execution context, while Test.stopTest() ensures that asynchronous operations execute before assertions.Your debugging explanation covers System.debug(), but filtering logs in Developer Console is crucial for analyzing large logs effectively.
System.debug('Processing Account: ' + acc.Id); // Only shows debug-level logs
"How can you filter logs in Developer Console to show only System.debug() messages?"
Salesforce DX (SFDX) is a command-line interface for managing metadata deployment and version control.
sfdx force:auth:web:login -r https://login.salesforce.com
sfdx force:source:push -u MyDevOrg
sfdx force:source:pull -u MyDevOrg
sfdx force:source:retrieve -m ApexClass
sfdx force:source:deploy -m ApexClass:MyClass
sfdx force:mdapi:deploy:report -u MyDevOrg
sfdx force:apex:test:run -u MyDevOrg -r human
sfdx force:org:delete -u MyScratchOrg
"What SFDX command is used to deploy local metadata to Salesforce?"
sfdx force:source:push -u MyDevOrg| Tool | Functionality |
|---|---|
| GitHub Actions | Automates deployments with version control |
| Jenkins | Customizable automation for deployments |
| Bitbucket Pipelines | CI/CD workflows for Git-based repositories |
| Azure DevOps | Integrated CI/CD for large-scale enterprise use |
name: Deploy to Salesforce
on: push
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Deploy to Salesforce
run: sfdx force:source:deploy -p force-app -u MyDevOrg
"What is CI/CD in Salesforce development?"
Why must Apex code have at least 75% test coverage before deployment?
Salesforce requires at least 75% code coverage to ensure Apex code is properly tested before being deployed to production.
Test coverage measures how much of the Apex code is executed by test classes. Salesforce enforces a minimum coverage threshold to reduce the risk of deploying untested code that could cause system failures.
Although the requirement is 75%, developers should aim for higher coverage to ensure critical logic is fully validated. Test classes simulate real scenarios and verify that the code behaves as expected under different conditions.
However, coverage alone does not guarantee code quality. Developers must also include meaningful assertions to verify results rather than simply executing code paths.
Demand Score: 94
Exam Relevance Score: 95
What is the purpose of Test.startTest() and Test.stopTest() in Apex test classes?
They reset governor limits and ensure asynchronous code executes during the test.
Test.startTest() marks the beginning of the section of code being tested. When this method is called, Salesforce resets governor limits so the code being tested has a fresh set of limits.
Test.stopTest() marks the end of the test execution block. When it runs, Salesforce executes any asynchronous operations such as future methods, queueable jobs, or batch processes.
This mechanism ensures that asynchronous logic can be properly tested inside Apex test classes. Developers frequently use this pattern to verify behavior involving asynchronous processing or large data operations.
Demand Score: 88
Exam Relevance Score: 93
Why should developers avoid using existing organization data in test classes?
Because tests must be independent and repeatable regardless of the data in the environment.
Salesforce test classes run in an isolated environment where existing data is not available by default. This ensures that tests behave consistently across different environments such as sandboxes and production.
Developers must create their own test data within the test class instead of relying on records already stored in the database. This approach guarantees that the test results remain predictable and reproducible.
Using independent test data also prevents failures caused by changes in production data. Salesforce provides tools like Test.loadData() and factory methods to simplify creating consistent test records.
Demand Score: 86
Exam Relevance Score: 91
What is the purpose of assertions in Apex test classes?
Assertions verify that the code produces the expected results.
Assertions are statements used in test classes to confirm that the actual output of a method matches the expected outcome. Without assertions, a test may run successfully even if the logic produces incorrect results.
For example, after inserting records or calling a method, a developer might check that a field value changed correctly or that a related record was created.
Assertions ensure that tests validate behavior rather than simply executing code. Salesforce encourages developers to write meaningful assertions to improve the reliability of their applications and detect logic errors early in the development process.
Demand Score: 82
Exam Relevance Score: 88
Why might a deployment fail even if all tests pass?
Because the overall organization code coverage may still be below the required 75%.
During deployment, Salesforce evaluates code coverage across the entire organization rather than just the classes included in the deployment package. If other classes in the organization reduce the overall coverage below the required threshold, the deployment will fail.
This situation often occurs when legacy classes have little or no test coverage. Developers must ensure that enough tests exist across the system to maintain the required minimum coverage level.
Understanding how Salesforce calculates coverage helps developers diagnose deployment failures during continuous integration and release processes.
Demand Score: 85
Exam Relevance Score: 90
What tool helps developers debug Apex code during execution?
The Salesforce Debug Log.
Debug logs record detailed information about transactions executed within Salesforce. Developers can view these logs to analyze how Apex code runs, identify errors, and track variable values during execution.
Logs capture events such as method calls, SOQL queries, DML operations, and exceptions. By reviewing these logs, developers can trace the sequence of operations and determine where a problem occurred.
Debug logs are particularly useful when troubleshooting triggers, asynchronous jobs, or automation processes involving multiple components.
Demand Score: 80
Exam Relevance Score: 86