PDI · Question #100
A developer needs to create a baseline set of data (Account, Contacts, Products, Assets) for an entire suite allowing them to test independent requirements for various types of Salesforce Cases…
The correct answer is A. Use &TestSteup with a void method. The question asks for the most efficient way to generate a common baseline set of data for multiple Apex unit tests.
Question
A developer needs to create a baseline set of data (Account, Contacts, Products, Assets) for an entire suite allowing them to test independent requirements for various types of Salesforce Cases. Which approach can efficiently generate the required data for each unit test?
Options
- AUse &TestSteup with a void method.
- BCreate test data before Test.startTest in the unit test.
- CCreate a mock using the Stub APL.
- DAdd $ IsTest (seeAllDatatrue) at the start of the unit test class.
How the community answered
(15 responses)- A80% (12)
- B7% (1)
- C13% (2)
Why each option
The question asks for the most efficient way to generate a common baseline set of data for multiple Apex unit tests.
Using the `@TestSetup` annotation with a `void` method allows common test records to be created once for an entire test class, making them available to all test methods and significantly improving test execution efficiency by avoiding repetitive data creation.
Creating test data within each individual unit test method is less efficient as it duplicates data creation efforts and increases overall test execution time.
Creating a mock using the Stub API is for isolating external dependencies and simulating responses, not for efficiently generating actual baseline records for DML operations within unit tests.
Using `@IsTest(seeAllData=true)` allows access to existing organization data, which is discouraged for unit testing best practices due to potential test inconsistency and fragility, and it does not generate new test data.
Concept tested: Efficient Apex test data setup
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_unittest_method.htm#apex_testing_unittest_method_testsetup
Topics
Community Discussion
No community discussion yet for this question.