PDI · Question #148
A developer is creating a test coverage for a class and needs to insert records to validate functionality. Which method annotation should be used to create records for every method in the test class?
The correct answer is C. @TestSetup. The @TestSetup annotation should be used on a method to create test records once for a test class, making them available to all test methods in that class for efficient and consistent data setup.
Question
A developer is creating a test coverage for a class and needs to insert records to validate functionality. Which method annotation should be used to create records for every method in the test class?
Options
- A@BeforeTest
- B@isTest(SeeAllData=True)
- C@TestSetup
- D@PreTest
How the community answered
(34 responses)- A6% (2)
- C91% (31)
- D3% (1)
Why each option
The `@TestSetup` annotation should be used on a method to create test records once for a test class, making them available to all test methods in that class for efficient and consistent data setup.
`@BeforeTest` is not a valid Apex test annotation.
`@isTest(SeeAllData=True)` allows tests to access existing organization data but does not create new records and is generally discouraged for isolated unit tests.
The `@TestSetup` annotation defines a method that executes once before any test methods in the test class, creating a common set of test records that are then shared and rolled back after each individual test method execution, optimizing test data creation.
`@PreTest` is not a valid Apex test annotation.
Concept tested: Efficient test data setup with @TestSetup
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_method.htm
Topics
Community Discussion
No community discussion yet for this question.