Testing: 12% Flashcards
What annotation is used to define classes or individual methods that only contain code used for testing
@isTest
Unit test methods are static methods that are defined with the @isTest annotation or the testMethod keyword
What are the two options for creating test data for a test class?
1- Create test data in your test method
2- Write utility test classes containing methods for test data creation that can be called by other tests
Why should you add @isTest annotation to a common utility class used by test classes?
Using this annotation is that the class won’t count against the 3MB organization code size limit. The public methods in this can only be called by test code.
Why is using Test.startTest and Test.stopTest statements a good testing practice?
You can perform prerequisite setup and test data creation prior to calling the Test.startTest and include verifications after Test.stopTest. The original set of governor limits are reverted to after the call to Test.stopTest
When running test classes, why should you set the Test | Always Run Asynchronously flag?
If you don’t select Always Run Asynchronously, test runs that include tests from only one class run synchronously. Test runs that include more than one class run asynchronously even if you don’t select this option
When would a test method be public versus private?
It’s a best practice to keep your test code private, but if you create test helper or utility classes that are used by separate test classes, they’ll need to be public.
The reason for private classes are that the Apex test framework can find and run your tests, but nothing else should be able to.
What is a good pattern to follow for writing test classes (3 easy steps :))
1- Perform required setup, including creation of test data
2- Perform the test execution, wrapped inside Test.startTest() and Test.endTest() test framework calls
3- Compare the results of the test execution with known data. (Compare actual behaviour to expected behaviour.)
It’s also best practice to test only one thing at a time, and to put each test into a separate method.
What is Test-first development?
It is the practice of writing tests for a feature before you write the code to implement the feature.
Because the feature isn’t implemented yet, the tests will fail. Then you implement the feature and run the tests again. When they pass, you have some confidence that you’ve implemented the feature correctly.
When testing controllers is it necessary for all page names to be in lowercase or is it case insensitive?
All page names must be in lowercase for all test methods
Which constructor should be used when testing extensions to standard list controllers?
ApexPages.StandardSetController(new ObjectName[])
When testing Controllers, how can controllers mimic navigating to another page?
Through the following code:
controller.save().getURL();
Which method is used to populate test data via a static resource
Test.loadData
If the static resource name is myResource, then the code will look as follows:
List<sObject> ls = Test.loadData(Account.sObjectType, 'myResource');</sObject>
What is the purpose of defining a test class with @isTest(SeeAllData=true)
It ensures that all the test methods in the class have access to all the data in the organization
What does the runAs Method do in Unit tests?
User record sharing is enforced.
It doesn’t enforce user permissions or field-level permissions, only record sharing
How can you bypass the mixed DML error in test methods?
1) You can use the runAs method to perform mixed DML operations in your test by enclosing the DML operations within the runAs block. In this way, you bypass the mixed DML error that is otherwise returned when inserting or updating setup objects together with other sObjects.
2) Use @future to Bypass the Mixed DML error in a Test Method.