Test Class Methods
Test Class Methods
Dhananjay Aher
Test Class
Contains methods related to Apex tests.
Namespace
System
Test Methods
The following are methods for Test. All methods are static.
calculatePermissionSetGroup(psgIds)
Calculates aggregate permissions in specified permission set groups for testing.
calculatePermissionSetGroup(psgId)
Calculates aggregate permissions in a specified permission set group for testing.
clearApexPageMessages()
Clear the messages on a Visualforce page while executing Apex test methods.
createStub(parentType, stubProvider)
Creates a stubbed version of an Apex class that you can use for testing. This method is
part of the Apex stub API. You can use it with the System.StubProvider interface to create
a mocking framework.
Usage
The createStub() method works together with the System.StubProvider interface. You
define the behavior of the stubbed object by implementing the StubProvider interface.
Then you create a stubbed object using the createStub() method. When you invoke
methods on the stubbed object, the handleMethodCall() method of
the StubProvider interface is called to perform the behavior of the stubbed method.
enableChangeDataCapture()
Use this method in an Apex test so that change event notifications are generated for all
supported Change Data Capture entities. Call this method at the beginning of your test
before performing DML operations and calling Test.getEventBus().deliver();.
Usage
The enableChangeDataCapture() method ensures that Apex tests can fire change event
triggers regardless of the entities selected in Setup in the Change Data Capture page.
The enableChangeDataCapture() method doesn’t affect the entities selected in Setup.
enqueueBatchJobs(numberOfJobs)
Adds the specified number of jobs with no-operation contents to the test-context queue.
It first fills the test batch queue, up to the maximum 5 jobs, and then places jobs in the
test flex queue. It throws a limit exception when the number of jobs in the test flex queue
exceeds the allowed limit of 100 jobs.
Usage
Use this method to reduce testing time. Instead of using your org's real batch jobs for
testing, you can use this method to simulate batch-job enqueueing.
Using enqueueBatchJobs(numberOfJobs) is faster than enqueuing real batch jobs.
getEventBus()
Returns an instance of the test event bus broker, which lets you operate on platform
event or change event messages in an Apex test. For example, you can
call Test.getEventBus().deliver() to deliver event messages.
getFlexQueueOrder()
Returns an ordered list of job IDs for jobs in the test-context flex queue. The job at
index 0 is the next job slated to run. This method returns only test-context results, even if
it’s annotated with @IsTest(SeeAllData=true).
getStandardPricebookId()
Returns the ID of the standard price book in the organization.
Usage
This method returns the ID of the standard price book in your organization regardless of
whether the test can query organization data. By default, tests can’t query organization
data unless they’re annotated with @isTest(SeeAllData=true).
Creating price book entries with a standard price requires the ID of the standard price
book. Use this method to get the standard price book ID so that you can create price
book entries in your tests.
invokeContinuationMethod(controller, request)
Invokes the callback method for the specified controller and continuation in a test
method.
Usage
isRunningTest()
Returns true if the currently executing code was called by code contained in a test
method, false otherwise. Use this method if you need to run different code depending on
whether it was being called from a test.
loadData(sObjectToken, resourceName)
Inserts test records from the specified static resource .csv file and for the specified
sObject type, and returns a list of the inserted sObjects.
Usage
You must create the static resource prior to calling this method. The static resource is a
comma-delimited file ending with a .csv extension. The file contains field names and
values for the test records. The first line of the file must contain the field names and
subsequent lines are the field values. To learn more about static resources, see “Defining
Static Resources” in the Salesforce online help.
Once you create a static resource for your .csv file, the static resource will be assigned a
MIME type. Supported MIME types are:
text/csv
application/vnd.ms-excel
application/octet-stream
text/plain
newSendEmailQuickActionDefaults(contextId, replyToId)
Creates a new QuickAction.SendEmailQuickActionDefaults instance for testing a class
implementing the QuickAction.QuickActionDefaultsHandler interface.
setContinuationResponse(requestLabel, mockResponse)
Sets a mock response for a continuation HTTP request in a test method.
Usage
setCreatedDate(recordId, createdDatetime)
Sets CreatedDate for a test-context sObject.
Usage
All database changes are rolled back at the end of a test. You can’t use this method on
records that existed before your test executed. You also can’t use setCreatedDate in
methods annotated with @isTest(SeeAllData=true), because those methods have access
to all data in your org. If you set CreatedDate to a future value, it can cause unexpected
results. This method takes two parameters—an sObject ID and a Datetime value—neither
of which can be null.
setCurrentPage(page)
A Visualforce test method that sets the current PageReference for the controller.
setCurrentPageReference(page)
A Visualforce test method that sets the current PageReference for the controller.
setFixedSearchResults(fixedSearchResults)
Defines a list of fixed search results to be returned by all subsequent SOSL statements in
a test method.
setMock(interfaceType, instance)
Sets the response mock mode and instructs the Apex runtime to send a mock response
whenever a callout is made through the HTTP classes or the auto-generated code from
WSDLs.
To mock a callout if the code that performs the callout is in a managed package,
call Test.setMock from a test method in the same package with the same namespace.
setReadOnlyApplicationMode(applicationMode)
Sets the application mode for an organization to read-only in an Apex test to simulate
read-only mode during Salesforce upgrades and downtimes. The application mode is
reset to the default mode at the end of each Apex test run.
startTest()
Marks the point in your test code when your test actually begins. Use this method when
you are testing governor limits.
stopTest()
Marks the point in your test code when your test ends. Use this method in conjunction
with the startTest method.
testUninstall(uninstallImplementation)
Tests the implementation of the UninstallHandler interface, which is used for specifying
an uninstall script in packages. Tests run as the test initiator in the development
environment.
THANK
YOU DHANANJAY AHER