PDI · Question #47
Which two statements are true about using the @testSetup annotation in an Apex test class? Choose 2 answers
The correct answer is B. Qo The @testSetup annotation is not supported when the GisTest(SeeAllData=True) annotation D. A method defined with the @testSetup annotation executes once for each test method in the test. The @testSetup annotation is not compatible with SeeAllData=true and creates a dataset that is effectively refreshed for each test method, even though the setup method itself runs once per class.
Question
Which two statements are true about using the @testSetup annotation in an Apex test class? Choose 2 answers
Options
- ARecords created in the test setup method cannot be updated in individual test methods.
- BQo The @testSetup annotation is not supported when the GisTest(SeeAllData=True) annotation
- CTest data is inserted once for all test methods in a class.
- DA method defined with the @testSetup annotation executes once for each test method in the test
How the community answered
(48 responses)- A4% (2)
- B92% (44)
- C4% (2)
Why each option
The `@testSetup` annotation is not compatible with `SeeAllData=true` and creates a dataset that is effectively refreshed for each test method, even though the setup method itself runs once per class.
Records created within a `@testSetup` method can indeed be updated, deleted, or manipulated by individual test methods, as each test method operates on its own independent copy of the setup data.
The `@testSetup` annotation cannot be used in conjunction with the `@IsTest(SeeAllData=true)` annotation because `@testSetup` is designed for creating controlled, isolated test data, which conflicts with `SeeAllData=true`'s purpose of exposing all existing organization data.
Test data created by a `@testSetup` method is inserted into the database only once at the beginning of the entire test class execution, not once for each test method, which optimizes performance by reducing DML operations.
Although a method defined with the `@testSetup` annotation physically executes only once per test class, Salesforce effectively provides each individual test method in that class with a fresh copy of the data created by the `@testSetup` method. This ensures test method isolation, as changes made in one test method do not persist for subsequent test methods.
Concept tested: Apex Test Setup Methods (@testSetup)
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_setUp_data.htm
Topics
Community Discussion
No community discussion yet for this question.