Testing, Debugging and Deployment Flashcards
Unit tests for Apex classes are not created just so that Apex code can simply be deployed to production but, most importantly, to validate that the Apex classes work as expected. Which option represents a recommended approach to performing Apex unit tests?
A. Annotate @SeeAllData=true on the test class. Execute the methods to be tested and verify the results using assertions.
B. Annotate @SeeAllData=true on the test class. Execute the methods to be tested and verify the results using System.debug();
C. Create valid test data for testing. Execute the methods to be tested and verify the expected results using assertions.
D. Query test data on the organization. Execute the methods to be tested and verify the expected results using assertions.
✅ C. Create valid test data for testing. Execute the methods to be tested and verify the expected results using assertions.
🧠 Explanation:
The recommended practice for Apex unit testing is to create your own test data within the test class. This ensures the test is independent, predictable, and not dependent on external or production data. Assertions should be used to verify expected results. This improves test reliability and ensures tests remain valid across orgs and over time.
❌ Why the other options are incorrect:
- A and B: Using @SeeAllData=true is discouraged because it makes the test depend on existing data in the org, which may change. Also, System.debug()
does not verify anything — it’s for inspection only, not testing.
- D: Querying org data (without creating it in the test) leads to unreliable tests. Salesforce recommends not relying on org data for unit tests.
🔗 Documentation:
- Apex Testing Best Practices
- Test Class Data Isolation (@isTest and SeeAllData)
📚 Additional Resources:
- Trailhead: Apex Testing
- Apex Unit Testing Video Guide (YouTube)
Which are true regarding the runAs() method?
Choose 2 answers.
A. runAs() ignores user license limits
B. runAs() only enforces record sharing
C. runAs() can be used with existing users or a new user
D. runAs() can be used in any Apex method
✅ A. runAs() ignores user license limits
✅ C. runAs() can be used with existing users or a new user
🧠 Explanation:
- A. Correct: The runAs()
method is designed for test context only and does not enforce user license limits. You can create test users in your code and run code as that user even if the org doesn’t have available licenses.
- C. Correct: You can use runAs()
with either a user that already exists or one you create programmatically with insert
in your test context.
❌ Why the others are incorrect:
- B. Incorrect: runAs()
enforces both record sharing and object-level permissions in test methods, not only record sharing.
- D. Incorrect: runAs()
can only be used in test methods. It cannot be used in regular Apex code that runs outside test context.
🔗 Documentation:
- Testing Apex with runAs()
- User Context in Tests
📚 Additional Resources:
- Trailhead: Apex Testing
- runAs() method explained (blog)
Using the runAs Method
A Salesforce developer needs to deploy a package from a UAT environment to a production org that has the following components:
7 Custom Objects
43 Custom Fields
15 Validation Rules
20 Apex Classes
5 Apex Triggers
The production org runs on API version 33.0. What will happen during deployment when the default test is executed for the package?
A. No tests are run during the deployment.
B. All local tests are run during the deployment.
C. Running test classes are not required.
D. All local and managed package tests are run during the deployment.
✅ B. All local tests are run during the deployment.
🧠 Explanation:
Since the production org runs on API version 33.0, and the default test level is used during deployment, only local tests are executed — this means tests that are part of the code being deployed and any existing code in the target org not part of a managed package.
This behavior is governed by Test Levels in Metadata Deployments.
❌ Why the other options are incorrect:
- A: Incorrect. Tests are required for deployment of Apex code and triggers to production.
- C: Incorrect. Test classes are required for deployment unless using specific test levels (like RunSpecifiedTests or RunNoTests) which is not the case here.
- D: Incorrect. Managed package tests are not run unless using RunAllTestsInOrg, which is not the default.
🔗 Documentation:
- Deploying Metadata with Test Levels
- Testing and Code Coverage Requirements
📚 Additional Resources:
- Trailhead: Develop with Apex
Using the runAs Method
Which of the following activities are involved when performing unit testing in Apex?
Choose 1 answer.
A. Deleting test data after testing has been completed
B. Inspecting code coverage after running unit tests
C. Having enough licenses for users created via runAs()
D. Ensuring that testers have Visual Studio Code installed
✅ B. Inspecting code coverage after running unit tests
🧠 Explanation:
Inspecting code coverage is a key step in Apex unit testing. Salesforce requires 75% code coverage in production deployments that include Apex. After running tests, developers review code coverage to ensure this requirement is met and to identify untested areas.
❌ Why the other options are incorrect:
- A: Test data is not persisted after test execution. It’s automatically rolled back, so there’s no need to manually delete it.
- C: runAs()
ignores license limits during testing, so you don’t need real licenses for test users.
- D: Visual Studio Code is a recommended tool for development, but it’s not required to perform unit testing in Apex.
🔗 Documentation:
- Code Coverage in Apex Testing
- Best Practices for Apex Testing
📚 Additional Resources:
- Trailhead: Apex Testing
- Salesforce Help: Monitor and Improve Code Coverage
What is true regarding the Apex testing framework?
Choose 3 answers.
A. Apex test classes can access org data such as leads by default.
B. Unit tests can test single and bulk actions.
C. Unit tests cannot perform callouts to external web services.
D. Unit tests cannot send outbound email.
E. Records inserted by unit tests must be deleted.
✅ B. Unit tests can test single and bulk actions.
✅ C. Unit tests cannot perform callouts to external web services.
✅ D. Unit tests cannot send outbound email.
🧠 Explanation:
- B. Correct: Apex test methods should test both single-record and bulk operations, especially when testing triggers or batch classes.
- C. Correct: By default, callouts are not allowed in test methods. You must use Test.startTest()
with HttpCalloutMock
or similar to simulate them.
- D. Correct: Outbound email cannot actually be sent in tests. You can use the Messaging.sendEmail()
method, but the email will not go out — this avoids unintended real communications.
❌ Why the other options are incorrect:
- A: By default, test methods do not have access to org data unless annotated with @isTest(SeeAllData=true)
, which is discouraged.
- E: Test data is automatically rolled back after test execution. You do not need to manually delete it.
🔗 Documentation:
- Apex Testing Framework Overview
- Testing Callouts
- Testing Bulk Triggers
📚 Additional Resources:
- Trailhead: Apex Testing
- Salesforce Help: Apex Test Classes Best Practices
What is the procedure for unit testing?
Choose 1 answer.
A. Create test data, run all tests, verify results, delete any records created
B. Create test data, call the method being tested, verify results
C. Create test data, run all tests, delete any records created, and verify the results
D. Create test data, call the method being tested, verify results, delete any records created
✅ B. Create test data, call the method being tested, verify results
🧠 Explanation:
The typical unit testing process in Apex is to:
1. Create test data
2. Call the method being tested
3. Use assertions to verify results
You don’t need to delete test data because Apex automatically rolls back any data created in tests, keeping your org clean.
❌ Why the other options are incorrect:
- A, C, D: All mention deleting records, which is unnecessary in Apex testing. Tests run in a separate context and data is not committed to the database.
🔗 Documentation:
- Apex Testing Overview
- Best Practices for Test Methods
📚 Additional Resources:
- Trailhead: Apex Testing
- Apex Testing Patterns Video (YouTube)
Which of the following statements about running Apex test classes in the Developer Console are true?
Choose 3 answers.
A. An Overall Code Coverage pane is available that displays the percentage of code coverage for each class in org.
B. If the test run includes more than one test class, the Developer Console always runs tests asynchronously in the background.
C. The [Rerun Failed Tests] option reruns only the failed tests from the test run that are highlighted in the Tests tab.
D. The [Suite Manager] option is used to abort the test selected in the Tests tab.
E. The [New Suite] option is used to create multiple test classes at the same time.
✅ A. An Overall Code Coverage pane is available that displays the percentage of code coverage for each class in org.
✅ B. If the test run includes more than one test class, the Developer Console always runs tests asynchronously in the background.
✅ C. The [Rerun Failed Tests] option reruns only the failed tests from the test run that are highlighted in the Tests tab.
🧠 Explanation:
- A. Correct: The Developer Console provides an Overall Code Coverage panel to review coverage across all classes.
- B. Correct: When running multiple test classes, the Developer Console executes the tests asynchronously in the background, allowing you to continue working.
- C. Correct: The [Rerun Failed Tests] button reruns only the tests that failed in the last execution and are highlighted.
❌ Why the other options are incorrect:
- D: The Suite Manager is used to manage test suites, not to abort tests.
- E: The New Suite option creates a test suite (group of existing test classes), not new test classes.
🔗 Documentation:
- Developer Console Overview
- Run Tests in Developer Console
📚 Additional Resources:
- Trailhead: Apex Testing
- Salesforce Help: Developer Console Code Coverage
A developer has written the ‘AccountProcessor’ Apex class below to update the ‘Rating’ field of an account record if the value of its ‘Credit_Status__c’ field is ‘Excellent’. If ‘acc’ is the name of an account variable used for testing in a test method created for this class, which of the following statements can be used to test for a negative scenario?
Choose 1 answer.
public class AccountProcessor {
publ``ic static void accountMethod(Account acc) {
if (acc.Credit_Status__c == ‘Excellent’) {
acc.Rating = ‘Hot’;
update acc;
}
}
}
A. System.assertEquals(‘Hot’, acc.Rating);
B. System.assert(acc.Rating == ‘Cold’);
C. System.assertNotEquals(‘Cold’, acc.Rating);
D. System.assert(acc.Rating != ‘Hot’);
✅ D. System.assert(acc.Rating != ‘Hot’);
🧠 Explanation:
To test a negative scenario, we want to verify that the condition does not trigger. In this case, if Credit_Status__c is not ‘Excellent’, the method should not update the Rating field to ‘Hot’.
- ✅ D is correct because it ensures that the method did NOT apply the logic (i.e., acc.Rating is not ‘Hot’).
❌ Why the other options are incorrect:
- A asserts a positive case, not a negative one.
- B assumes a default ‘Cold’ value, which the method does not assign.
- C asserts a value not being ‘Cold’, which doesn’t prove negative behavior.
🔗 Documentation:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_isTest.htm
https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_system.htm
📚 Additional Resources:
- Trailhead: Apex Testing – https://trailhead.salesforce.com/content/learn/modules/apex_testing
- Negative Testing in Apex – https://salesforce.stackexchange.com/questions/133573/how-to-write-negative-test-case-in-apex
A developer of Cosmic Solutions would like to run all unit tests in Salesforce. Which of the following options are available?
Choose 2 answers.
A. Selecting the ‘Run All’ menu option in Data Loader.
B. Selecting the ‘Run All’ menu option in the Developer Console.
C. Using the ‘Run All Tests’ button on the ‘Test Classes’ page in Salesforce Setup.
D. Using the ‘Run All Tests’ button on the ‘Apex Classes’ page in Salesforce Setup.
✅ B. Selecting the ‘Run All’ menu option in the Developer Console.
✅ D. Using the ‘Run All Tests’ button on the ‘Apex Classes’ page in Salesforce Setup.
🧠 Explanation:
- B. Correct: The Developer Console provides a ‘Run All’ option under the Test menu, allowing developers to run all unit tests.
- D. Correct: The Apex Classes page in Setup includes a ‘Run All Tests’ button, enabling the execution of all test methods in the org.
❌ Why the other options are incorrect:
- A: Data Loader is a data import/export tool and does not support running tests.
- C: There is no ‘Run All Tests’ button on the ‘Test Classes’ page; the proper interface is through Apex Classes or the Apex Test Execution page.
🔗 Documentation:
- Run Tests in Developer Console
- Running Apex Tests from Setup
📚 Additional Resources:
- Trailhead: Apex Testing
- Salesforce: Monitor Test Execution
What is true regarding unit tests?
Choose 1 answer.
A. Once test coverage of 75% is reached, no more unit tests should be created
B. 100% of code in triggers must be covered by unit tests
C. All code must execute without throwing any uncaught exceptions
D. Governor limits are not applied to test methods
✅ C. All code must execute without throwing any uncaught exceptions
🧠 Explanation:
- C. Correct: A fundamental requirement for unit tests is that no uncaught exceptions should occur during execution. This ensures that the logic behaves as expected under test conditions and is stable enough for production deployment.
❌ Why the other options are incorrect:
- A: Incorrect. Reaching 75% coverage is the minimum required for deployment, but more tests should be created to ensure robustness and cover edge cases.
- B: Incorrect. Triggers must be included in the overall 75% coverage, but there’s no rule requiring 100% coverage.
- D: Incorrect. Governor limits are applied during test execution to ensure your code respects platform constraints.
🔗 Documentation:
- Apex Code Coverage
- Best Practices for Apex Testing
📚 Additional Resources:
- Trailhead: Apex Testing
- Salesforce Help: Understanding Apex Test Results
A developer has found that test data creation is duplicated across test classes. How can this be eliminated?
Choose 1 answer.
A. De-annotating @isTest on methods
B. Using one common test class
C. Creating a common test utility class
D. Defining test methods as public
✅ C. Creating a common test utility class
🧠 Explanation:
- C. Correct: The best practice to avoid duplicated test data is to use a test utility class with reusable methods that generate standard test records. These utility methods can be public static
and called from multiple test classes.
❌ Why the other options are incorrect:
- A: Removing @isTest
will make the method no longer recognized as a test — this is not a solution.
- B: While a common test class might help, it’s not scalable or flexible. Utility classes are a cleaner solution.
- D: Test methods should be annotated with @isTest
and defined as private
, not public. Public access is unnecessary and not recommended for test methods.
🔗 Documentation:
- Creating Test Utility Classes
- Apex Test Classes
📚 Additional Resources:
- Trailhead: Apex Testing
- Salesforce Help: Reusable Test Utilities
A developer is selecting and running the same set of test classes repeatedly. What feature can help with making this task more efficient?
Choose 1 answer.
A. Defining a test suite
B. Defining a test set
C. Defining a master test class
D. Reducing the number of test classes
✅ A. Defining a test suite
🧠 Explanation:
- A. Correct: A test suite in the Developer Console allows a developer to group a set of test classes and run them together. This is ideal for recurring test runs and saves time by avoiding manual selection.
❌ Why the other options are incorrect:
- B: “Test set” is not a recognized feature in the Salesforce Developer Console or setup tools.
- C: A “master test class” is not an official concept. While you could centralize logic, it wouldn’t help with selecting classes.
- D: Reducing the number of test classes is not a solution — it could compromise test coverage and quality.
🔗 Documentation:
- Run Tests in Developer Console
- Create and Manage Test Suites
📚 Additional Resources:
- Trailhead: Apex Testing
- Video: Salesforce Test Automation Tips
What steps are required to create test data from files using static resources?
Choose 2 answers.
A. Call Test.loadData() within the test method.
B. Create the test data in a .csv file and upload it as a static resource.
C. Create a static resource and edit to add test data.
D. Call System.loadData within the test method.
✅ A. Call Test.loadData() within the test method.
✅ B. Create the test data in a .csv file and upload it as a static resource.
🧠 Explanation:
- A. Correct: Test.loadData()
is a method used in test methods to load data from a .csv
file uploaded as a static resource.
- B. Correct: The .csv
file containing the test data must be created and uploaded as a static resource so that it can be used with Test.loadData()
.
❌ Why the other options are incorrect:
- C: Static resources cannot be “edited” directly to add data; you upload them with the content already defined.
- D: System.loadData
does not exist — the correct method is Test.loadData
.
🔗 Documentation:
- Test.loadData() Documentation
- Working with Static Resources
📚 Additional Resources:
- Trailhead: Apex Testing
- Video: Load Test Data with Static Resources
A developer needs to determine the exact amount of resources that have been consumed in a particular code block with regards to governor limits. Which of the following Test methods can be used in conjunction with Limits methods?
Choose 1 answer.
A. Test.isTest() and Test.isNotTest()
B. Test.getCurrentLimits() and Test.getLimits()
C. Test.beginTest() and Test.endTest()
D. Test.startTest() and Test.stopTest()
✅ D. Test.startTest() and Test.stopTest()
🧠 Explanation:
- D. Correct: Test.startTest()
and Test.stopTest()
are used to reset governor limits in test methods. When placed around a block of code, they allow you to measure resource consumption accurately by starting fresh limits.
❌ Why the other options are incorrect:
- A: These methods do not exist.
- B: Also not real Apex test methods — Limits
is the class used to check limits, not Test.getCurrentLimits()
.
- C: Test.beginTest()
and Test.endTest()
are incorrect names. The correct methods are startTest()
and stopTest()
.
🔗 Documentation:
- Test.startTest() and Test.stopTest()
- Using Limits Class
📚 Additional Resources:
- Trailhead: Apex Testing
- Salesforce Help: Monitor Governor Limits
Test methods don’t have access by default to pre-existing data in the organization except for which of the following?
Choose 3 answers.
A. Record Types
B. Custom Settings
C. Users
D. Custom Objects
E. Profiles
✅ A. Record Types
✅ C. Users
✅ E. Profiles
🧠 Explanation:
Apex test methods do not have access to real org data by default unless you annotate with @isTest(SeeAllData=true)
. However, there are exceptions where some metadata and setup data are accessible by default, even without SeeAllData.
- ✅ A. Record Types: These are metadata and are available to test methods without SeeAllData.
- ✅ C. Users: Although users are data records, Salesforce allows access to current user and admin-defined test users. Additionally, test methods can create and query users without SeeAllData.
- ✅ E. Profiles: These are setup data and are always available for querying in test methods.
❌ Why the others are incorrect:
- B. Custom Settings: While accessible in some cases, hierarchy custom settings may not return results in tests unless created within the test context.
- D. Custom Objects: These require you to insert test data. Records from the org are not accessible by default.
🔗 Documentation:
- Testing Data Visibility
- @isTest(SeeAllData) Annotation
📚 Additional Resources:
- Trailhead: Apex Testing
- Salesforce Help: Test Class Data Access
Which of the following statements are true regarding running tests from the Salesforce UI?
Choose 2 answers.
A. After test classes are selected, they are placed in the Apex Job queue and will always run in parallel
B. All test classes can be selected
C. Individual test classes can be selected
D. After tests are run, code coverage is available on Apex Test Execution page
✅ B. All test classes can be selected
✅ D. After tests are run, code coverage is available on Apex Test Execution page
🧠 Explanation:
- ✅ B. Correct: From the Salesforce UI, specifically in the Apex Test Execution section, developers can select all test classes to run in a batch.
- ✅ D. Correct: After the tests are executed, you can view code coverage details directly in the Apex Test Execution and Code Coverage pages.
❌ Why the other options are incorrect:
- A: While test classes are placed in the Apex Job queue, they do not always run in parallel — Salesforce may run them serially depending on system load or configuration.
- C: You cannot always run individual test methods via UI. You can select individual classes, but the option here is ambiguous when contrasted with B.
🔗 Documentation:
- Run Tests from Setup UI
- Code Coverage Viewer
📚 Additional Resources:
- Trailhead: Apex Testing
- Monitor Apex Tests
Which of the following statements are true about creating unit tests in Apex?
Choose 2 answers.
A. Use the System.assert() method to test your application in different user contexts.
B. Lines of code in Test methods and test classes are not counted as part of calculating Apex code coverage.
C. If code uses conditional logic (including ternary operators), one scenario will automatically cover all conditions.
D. Since data created in tests do not commit, you will not need to delete any data.
✅ B. Lines of code in Test methods and test classes are not counted as part of calculating Apex code coverage.
✅ D. Since data created in tests do not commit, you will not need to delete any data.
🧠 Explanation:
- ✅ B. Correct: Test classes and test methods do not count toward the Apex code coverage percentage. Only lines of code in non-test classes and triggers are measured.
- ✅ D. Correct: Data created within test methods is rolled back automatically after the test runs — no need to delete it manually.
❌ Why the others are incorrect:
- A: System.assert()
is used to verify expected outcomes, not for testing user context. Use System.runAs()
for that.
- C: Conditional logic needs multiple scenarios to be tested — ternary operators or if-else
blocks must be covered with different input values to reach all branches.
🔗 Documentation:
- Apex Code Coverage
- Test Data Lifespan
📚 Additional Resources:
- Trailhead: Apex Testing
- Video: Writing Effective Apex Unit Tests
Unit tests need to be created for testing new functionality added to an Apex class that is used as a controller of an account management tool. If the variable ‘a’ represents an Account record in the code snippet of a unit test below, which of the following options represents code that should be used to replace the placeholder comments?
Choose 2 answers.
a.addError(‘Site’, ‘Site is not accessible!’);
//Add code to assert that an error was added to the account instance
//Add code to retrieve any errors added to the account instance
System.assertEquals(errors.size(), 1);
✅ A. System.assertEquals(a.hasErrors(), true);
✅ B. List<Database.Error> errors = a.getErrors();
🧠 Explanation:
- ✅ **A. Correct**: `a.hasErrors()` is a method that checks if an SObject instance has any errors attached to it. It returns a boolean.
- ✅ **B. Correct**: `a.getErrors()` retrieves the list of `Database.Error` objects added to the record with `.addError()`. This is exactly what’s needed for the assertion `System.assertEquals(errors.size(), 1);`.</Database.Error>
❌ Why the others are incorrect:
- C: a.errors()
is not a valid method for SObject. The correct method is getErrors()
.
- D: a.containsErrors()
does not exist in Apex — it’s an invalid method.
🔗 Documentation:
- SObject.addError()
- SObject.getErrors()
- SObject.hasErrors()
📚 Additional Resources:
- Trailhead: Apex Testing
- Salesforce Help: Handling Validation Errors in Tests
Which of the following statements are true about skipping code coverage for an Apex test run in the Developer Console?
Choose 3 answers.
A. Skipping code coverage has no impact on the speed of a test run.
B. No data about Apex test coverage is stored.
C. The Suite Manager can be used to skip code coverage.
D. Faster feedback is received on the pass or fail status of a test run.
E. Code coverage can be skipped during a new test run.
✅ B. No data about Apex test coverage is stored.
✅ D. Faster feedback is received on the pass or fail status of a test run.
✅ E. Code coverage can be skipped during a new test run.
🧠 Explanation:
- ✅ B: When you skip code coverage during a test run, Salesforce does not store any code coverage data.
- ✅ D: Skipping coverage speeds up execution, which provides faster feedback on pass/fail results.
- ✅ E: In the Developer Console, there’s an option to skip code coverage when launching a new test run.
❌ Why the others are incorrect:
- A: Skipping code coverage does improve performance, so saying it has no impact is false.
- C: The Suite Manager is used to group test classes, not to skip coverage — that’s done through a checkbox in the Developer Console.
🔗 Documentation:
- Developer Console Test Execution
- Skip Code Coverage Option
📚 Additional Resources:
- Trailhead: Apex Testing
- Salesforce Help: Developer Console Overview
A developer would like to speed up test runs for a few Apex test classes by skipping the collection of code coverage information. Where can the option to skip code coverage be selected in a Salesforce org?
Choose 2 answers.
A. ‘Apex Classes’ page in Setup
B. ‘Test Manager’ in the Developer Console
C. ‘Apex Test Execution’ page in Setup
D. ‘New Run’ menu item in the Developer Console
✅ B. ‘Test Manager’ in the Developer Console
✅ D. ‘New Run’ menu item in the Developer Console
🧠 Explanation:
- ✅ B. Correct: The Test Manager in the Developer Console provides the interface to run tests, and includes an option to skip collecting code coverage.
- ✅ D. Correct: When you select ‘New Run’ in the Developer Console, you can choose to skip code coverage collection by checking the corresponding option.
❌ Why the others are incorrect:
- A: The ‘Apex Classes’ page does not provide test execution or any option related to code coverage.
- C: The ‘Apex Test Execution’ page allows you to run and monitor tests, but you cannot skip code coverage from there.
🔗 Documentation:
- Run Tests in the Developer Console
- Skip Code Coverage Option
📚 Additional Resources:
- Trailhead: Apex Testing
- Salesforce Help: Developer Console Overview
How can a developer execute a new test class?
Choose 2 answers.
A. Through the Test menu in the Developer Console
B. Through the Apex Test Execution in the Setup menu
C. Using the REST API and ApexTestRun method
D. By accessing the Run Apex Classes in the Setup menu
✅ A. Through the Test menu in the Developer Console
✅ B. Through the Apex Test Execution in the Setup menu
🧠 Explanation:
- ✅ A. Correct: In the Developer Console, you can run test classes using the Test > New Run menu item.
- ✅ B. Correct: From Setup, you can go to Apex Test Execution to schedule or immediately run selected test classes.
❌ Por qué las otras son incorrectas:
- C: While technically valid for automation, the REST API is not typically how a developer manually executes test classes.
- D: The ‘Run Apex’ page is for executing Apex code, not test classes.
🔗 Documentación:
- Run Apex Tests in Developer Console
- Run Apex Tests from Setup
📚 Recursos adicionales:
- Trailhead: Apex Testing
- Salesforce CLI + Apex Test Guide
When test data cannot be created programmatically, how can pre-existing data be accessed?
Choose 1 answer.
A. Annotate the test method with [WithSharing=false]
B. Annotate the test class or method with [SeeAllData=true]
C. Annotate the test class or method with [SeeAllData=false]
D. Annotate the test method with [WithSharing=true]
✅ B. Annotate the test class or method with [SeeAllData=true]
🧠 Explanation:
- ✅ B. Correct: By default, test methods do not have access to org data. To explicitly allow access to pre-existing records, use the annotation @isTest(SeeAllData=true)
on the class or method. This should be used sparingly, only when data cannot be created in the test context (e.g., Pricebook2, Person Accounts, etc.).
❌ Por qué las otras son incorrectas:
- A y D: with sharing
and without sharing
control record-level access enforcement — not access to org data.
- C: This is actually the default behavior — SeeAllData=false
is assumed unless overridden — and it does not allow access to org data.
🔗 Documentación:
- @isTest Annotation and SeeAllData
- Test Data Visibility
📚 Recursos adicionales:
- Trailhead: Apex Testing
- Salesforce Help: SeeAllData Explained
Which of the following capabilities are included in the Apex testing framework?
Choose 2 answers.
A. Check test results and code coverage.
B. Use integrated tools to run unit tests.
C. Create test methods automatically.
D. Audit code for security vulnerabilities.
✅ A. Check test results and code coverage.
✅ B. Use integrated tools to run unit tests.
🧠 Explanation:
- ✅ A. Correct: The Apex testing framework includes built-in functionality to verify test results and analyze code coverage after test execution.
- ✅ B. Correct: Salesforce provides several integrated tools like the Developer Console, Setup UI, and Salesforce CLI to run unit tests easily.
❌ Por qué las otras son incorrectas:
- C: Test methods must be written manually. The framework does not auto-generate test methods.
- D: Security auditing is not part of the Apex testing framework. That’s handled by other tools like Salesforce Code Analyzer or Checkmarx.
🔗 Documentación:
- Apex Testing Overview
- Apex Code Coverage
📚 Recursos adicionales:
- Trailhead: Apex Testing
- Salesforce Help: Developer Console for Tests
A developer is creating an Apex class that will rely heavily on a user’s profile and permission sets to determine which logic path to take. What testing best practices should the developer implement in the unit tests?
Choose 2 answers.
A. Test all conditional paths through the code to maximize coverage
B. Use the seeAllData=true modifier on Apex tests to avoid running into sharing issues
C. Use system.assert() methods to verify that data is created correctly
D. Run tests that result in no more than 10 records being updated at a time
✅ A. Test all conditional paths through the code to maximize coverage
✅ C. Use system.assert() methods to verify that data is created correctly
🧠 Explanation:
- ✅ A. Correct: It’s a best practice to test all logical branches to ensure high coverage and validate the behavior of permission- or profile-driven logic.
- ✅ C. Correct: Using System.assert()
is essential to verify expected outcomes, such as confirming the correct logic path was taken or that the data is valid.
❌ Por qué las otras son incorrectas:
- B: Using seeAllData=true
is discouraged. It breaks test isolation and leads to unreliable results. You should create any required data in the test method.
- D: There is no best practice limiting updates to 10 records in tests. In fact, you should test bulk operations with more than 10 to validate bulk-safe logic.
🔗 Documentación:
- Best Practices for Apex Tests
- System.assert() Method
📚 Recursos adicionales:
- Trailhead: Apex Testing
- Salesforce Help: Unit Test Structure