PDI · Question #230
A developer is asked to write helper methods that create test data for unit tests. What should be changed in the Testvtils class so that its methods are only usable by unit test methods?
The correct answer is B. Add @IsTest above line 03,. The question asks how to make helper methods in a utility class callable exclusively by unit test methods.
Question
A developer is asked to write helper methods that create test data for unit tests. What should be changed in the Testvtils class so that its methods are only usable by unit test methods?
Exhibit
Options
- AChange public to private on line 01.
- BAdd @IsTest above line 03,
- CAdd @IsTest above line 01.
- DRemove static from line 03.
How the community answered
(37 responses)- A3% (1)
- B86% (32)
- C3% (1)
- D8% (3)
Why each option
The question asks how to make helper methods in a utility class callable exclusively by unit test methods.
Changing `public` to `private` for the class would prevent any other classes, including test classes, from accessing its methods.
Adding `@IsTest` above a method in a non-test class makes that specific method a test utility method, visible and callable only from other test methods or test classes, ensuring it's not used in production code.
Adding `@IsTest` above the class definition would declare the entire class as a test class, which is not the specific goal of restricting individual helper methods within a potentially non-test utility class.
Removing `static` would require instantiating the class to call the method but would not restrict its callable context to only unit test methods.
Concept tested: @IsTest annotation for test utility methods
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_isTest.htm
Topics
Community Discussion
No community discussion yet for this question.
