How do I test asynchronous Apex?
This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through the resources here and use them to shape more specific questions. To browse all canonical questions and answers, including more unit test resources, navigate to the canonical-qa
tag.
I'm testing asynchronous code - like Batch Apex, Queueable Apex, @future
methods, or Scheduled Apex - and I'm getting unexpected results. It seems like portions of my code aren't running, or I'm getting confusing errors about batch invocations. How do I effectively test this kind of code?
unit-test asynchronous asynchronous-testing canonical-qa
add a comment |
This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through the resources here and use them to shape more specific questions. To browse all canonical questions and answers, including more unit test resources, navigate to the canonical-qa
tag.
I'm testing asynchronous code - like Batch Apex, Queueable Apex, @future
methods, or Scheduled Apex - and I'm getting unexpected results. It seems like portions of my code aren't running, or I'm getting confusing errors about batch invocations. How do I effectively test this kind of code?
unit-test asynchronous asynchronous-testing canonical-qa
add a comment |
This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through the resources here and use them to shape more specific questions. To browse all canonical questions and answers, including more unit test resources, navigate to the canonical-qa
tag.
I'm testing asynchronous code - like Batch Apex, Queueable Apex, @future
methods, or Scheduled Apex - and I'm getting unexpected results. It seems like portions of my code aren't running, or I'm getting confusing errors about batch invocations. How do I effectively test this kind of code?
unit-test asynchronous asynchronous-testing canonical-qa
This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through the resources here and use them to shape more specific questions. To browse all canonical questions and answers, including more unit test resources, navigate to the canonical-qa
tag.
I'm testing asynchronous code - like Batch Apex, Queueable Apex, @future
methods, or Scheduled Apex - and I'm getting unexpected results. It seems like portions of my code aren't running, or I'm getting confusing errors about batch invocations. How do I effectively test this kind of code?
unit-test asynchronous asynchronous-testing canonical-qa
unit-test asynchronous asynchronous-testing canonical-qa
asked 16 hours ago
community wiki
David Reed
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Asynchronous Apex includes all of the methods for executing code on the Salesforce platform outside a synchronous transaction, including:
@future
methods.- Batch Apex
- Queueable Apex
- Schedulable Apex
Because these constructs are by nature asynchronous, do not come with an SLA, and can be executed by Salesforce based upon overall system load and other considerations, we cannot typically guarantee exactly when they will be executed. This requires some changes to how we build and structure unit tests for code that is built within or makes use of any of the four asynchronous code types mentioned above.
Use of Test.startTest()
and Test.stopTest()
Because of the way Asynchronous Apex works, any asynchronous code - a future method is a useful example - will not be executed during the confines of an Apex unit test unless we take specific action. A unit test forms a single transaction, and asynchronous code enqueued within that transaction cannot be executed until the transaction commits successfully.
For this reason, Salesforce has provided a framework to force asynchronous code to execute synchronously for testing:
We enclose our test code between Test.startTest()
and Test.stopTest()
. The system collects all asynchronous calls made after startTest()
. When stopTest()
is executed, these collected asynchronous processes are then run synchronously and complete before control returns to our code.
Following Test.stopTest()
, our code can evaluate the results of the executed asynchronous code and make assertions to validate its behavior.
Test.startTest();
AsyncUtil.executeFutureMethod();
Test.stopTest();
System.assertEquals(expected, actualChangesInAsync);
Nested Asynchronous Code
The collection and synchronous execution of Asynchronous Apex applies only between Test.startTest()
and Test.stopTest()
. Any further asynchronous code that's enqueued by the asynchronous operations that are executed at Test.stopTest()
is not executed synchronously in the context of the unit test. For example, if we're working with the following code:
public class MySchedulable implements Schedulable {
private Account a;
public MySchedulable(Account a) {
this.a = a;
}
public void execute(SchedulableContext sc) {
a.Description = 'Contacted the customer');
update a;
Database.executeBatch(new ContactsUpdaterBatch(a), 200);
}
}
public class ContactsUpdaterBatch implements Database.Batchable<sObject> {
// ContactsUpdaterBatch updates the Description field on Contact (elided for brevity).
}
A unit test structured like this will not work:
@isTest
public static void updating_accounts_updates_contacts() {
Account a = [SELECT Id FROM Account LIMIT 1];
Test.startTest();
System.schedule('TEST_MySchedulable', '0 0 * * * ?', new MySchedulable(a));
Test.stopTest();
a = [SELECT Id, Description, (SELECT Id, Description FROM Contacts) FROM Account WHERE Id = :a.Id];
System.assertEquals('Contacted the customer', a.Description, 'found correct description');
for (Contact c : a.Contacts) {
System.assertEquals('Account has been contacted', c.Description, 'found correct Contact description');
}
}
The second assertion will fail, because the batch class ContactsUpdateBatch
, fired from within the asynchronous MySchedulable
, will not execute during test context - even though the first layer, MySchedulable.execute()
, is called at Test.stopTest()
.
The same pattern applies to other multi-layer asynchronous code, including @future
methods and Queueables.
There's no work-around to allow multi-level asynchronous code to execute in test context. Instead, the tests must be constructed to validate functionality without requiring this, by decomposing the tests to validate smaller units and/or using techniques like dependency injection to validate the connections between different asynchronous code units.
The example above can be effectively tested by decomposition: we can write separate unit tests against the Schedulable and the Batchable to validate their operation. The Schedulable test would validate the update to the Account and that a batch has been enqueued; the Batch test would validate the associated Contact updates.
Batch Class Execution
Unit test context permits only one batch execution (call to execute()
) to occur in a single unit test. While in most cases your unit tests would not insert more than one batch's worth of test data, it is possible to do so. This will result in an exception being thrown. Your unit test needs to guarantee that only one batch executes, either by controlling the batch size of the test data set or both.
Batches that execute across metadata objects, such as User
, are especially vulnerable to this challenge. While unit tests for these Batch classes may succeed in developer orgs or scratch orgs that have tiny record sets, they will fail when deployed to a larger production org. In many cases, these batches need at least a light dependency injection strategy to allow the unit test to control the queries executed by start()
.
For example, the query might be exposed in a @TestVisible
instance variable to allow a unit test to inject a more restricted query, or adding an Id
set to restrict the query results.
Resources
Trailhead Modules
Asynchronous Apex
- Includes modules on Future, Batch, Queueable, and Scheduled Apex, each of which describes testing approaches.
While multilayer asynchronous execution can't be asserted, the debug log may show that the child layer does execute leading to developer consternation.
– cropredy
6 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f244791%2fhow-do-i-test-asynchronous-apex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Asynchronous Apex includes all of the methods for executing code on the Salesforce platform outside a synchronous transaction, including:
@future
methods.- Batch Apex
- Queueable Apex
- Schedulable Apex
Because these constructs are by nature asynchronous, do not come with an SLA, and can be executed by Salesforce based upon overall system load and other considerations, we cannot typically guarantee exactly when they will be executed. This requires some changes to how we build and structure unit tests for code that is built within or makes use of any of the four asynchronous code types mentioned above.
Use of Test.startTest()
and Test.stopTest()
Because of the way Asynchronous Apex works, any asynchronous code - a future method is a useful example - will not be executed during the confines of an Apex unit test unless we take specific action. A unit test forms a single transaction, and asynchronous code enqueued within that transaction cannot be executed until the transaction commits successfully.
For this reason, Salesforce has provided a framework to force asynchronous code to execute synchronously for testing:
We enclose our test code between Test.startTest()
and Test.stopTest()
. The system collects all asynchronous calls made after startTest()
. When stopTest()
is executed, these collected asynchronous processes are then run synchronously and complete before control returns to our code.
Following Test.stopTest()
, our code can evaluate the results of the executed asynchronous code and make assertions to validate its behavior.
Test.startTest();
AsyncUtil.executeFutureMethod();
Test.stopTest();
System.assertEquals(expected, actualChangesInAsync);
Nested Asynchronous Code
The collection and synchronous execution of Asynchronous Apex applies only between Test.startTest()
and Test.stopTest()
. Any further asynchronous code that's enqueued by the asynchronous operations that are executed at Test.stopTest()
is not executed synchronously in the context of the unit test. For example, if we're working with the following code:
public class MySchedulable implements Schedulable {
private Account a;
public MySchedulable(Account a) {
this.a = a;
}
public void execute(SchedulableContext sc) {
a.Description = 'Contacted the customer');
update a;
Database.executeBatch(new ContactsUpdaterBatch(a), 200);
}
}
public class ContactsUpdaterBatch implements Database.Batchable<sObject> {
// ContactsUpdaterBatch updates the Description field on Contact (elided for brevity).
}
A unit test structured like this will not work:
@isTest
public static void updating_accounts_updates_contacts() {
Account a = [SELECT Id FROM Account LIMIT 1];
Test.startTest();
System.schedule('TEST_MySchedulable', '0 0 * * * ?', new MySchedulable(a));
Test.stopTest();
a = [SELECT Id, Description, (SELECT Id, Description FROM Contacts) FROM Account WHERE Id = :a.Id];
System.assertEquals('Contacted the customer', a.Description, 'found correct description');
for (Contact c : a.Contacts) {
System.assertEquals('Account has been contacted', c.Description, 'found correct Contact description');
}
}
The second assertion will fail, because the batch class ContactsUpdateBatch
, fired from within the asynchronous MySchedulable
, will not execute during test context - even though the first layer, MySchedulable.execute()
, is called at Test.stopTest()
.
The same pattern applies to other multi-layer asynchronous code, including @future
methods and Queueables.
There's no work-around to allow multi-level asynchronous code to execute in test context. Instead, the tests must be constructed to validate functionality without requiring this, by decomposing the tests to validate smaller units and/or using techniques like dependency injection to validate the connections between different asynchronous code units.
The example above can be effectively tested by decomposition: we can write separate unit tests against the Schedulable and the Batchable to validate their operation. The Schedulable test would validate the update to the Account and that a batch has been enqueued; the Batch test would validate the associated Contact updates.
Batch Class Execution
Unit test context permits only one batch execution (call to execute()
) to occur in a single unit test. While in most cases your unit tests would not insert more than one batch's worth of test data, it is possible to do so. This will result in an exception being thrown. Your unit test needs to guarantee that only one batch executes, either by controlling the batch size of the test data set or both.
Batches that execute across metadata objects, such as User
, are especially vulnerable to this challenge. While unit tests for these Batch classes may succeed in developer orgs or scratch orgs that have tiny record sets, they will fail when deployed to a larger production org. In many cases, these batches need at least a light dependency injection strategy to allow the unit test to control the queries executed by start()
.
For example, the query might be exposed in a @TestVisible
instance variable to allow a unit test to inject a more restricted query, or adding an Id
set to restrict the query results.
Resources
Trailhead Modules
Asynchronous Apex
- Includes modules on Future, Batch, Queueable, and Scheduled Apex, each of which describes testing approaches.
While multilayer asynchronous execution can't be asserted, the debug log may show that the child layer does execute leading to developer consternation.
– cropredy
6 hours ago
add a comment |
Asynchronous Apex includes all of the methods for executing code on the Salesforce platform outside a synchronous transaction, including:
@future
methods.- Batch Apex
- Queueable Apex
- Schedulable Apex
Because these constructs are by nature asynchronous, do not come with an SLA, and can be executed by Salesforce based upon overall system load and other considerations, we cannot typically guarantee exactly when they will be executed. This requires some changes to how we build and structure unit tests for code that is built within or makes use of any of the four asynchronous code types mentioned above.
Use of Test.startTest()
and Test.stopTest()
Because of the way Asynchronous Apex works, any asynchronous code - a future method is a useful example - will not be executed during the confines of an Apex unit test unless we take specific action. A unit test forms a single transaction, and asynchronous code enqueued within that transaction cannot be executed until the transaction commits successfully.
For this reason, Salesforce has provided a framework to force asynchronous code to execute synchronously for testing:
We enclose our test code between Test.startTest()
and Test.stopTest()
. The system collects all asynchronous calls made after startTest()
. When stopTest()
is executed, these collected asynchronous processes are then run synchronously and complete before control returns to our code.
Following Test.stopTest()
, our code can evaluate the results of the executed asynchronous code and make assertions to validate its behavior.
Test.startTest();
AsyncUtil.executeFutureMethod();
Test.stopTest();
System.assertEquals(expected, actualChangesInAsync);
Nested Asynchronous Code
The collection and synchronous execution of Asynchronous Apex applies only between Test.startTest()
and Test.stopTest()
. Any further asynchronous code that's enqueued by the asynchronous operations that are executed at Test.stopTest()
is not executed synchronously in the context of the unit test. For example, if we're working with the following code:
public class MySchedulable implements Schedulable {
private Account a;
public MySchedulable(Account a) {
this.a = a;
}
public void execute(SchedulableContext sc) {
a.Description = 'Contacted the customer');
update a;
Database.executeBatch(new ContactsUpdaterBatch(a), 200);
}
}
public class ContactsUpdaterBatch implements Database.Batchable<sObject> {
// ContactsUpdaterBatch updates the Description field on Contact (elided for brevity).
}
A unit test structured like this will not work:
@isTest
public static void updating_accounts_updates_contacts() {
Account a = [SELECT Id FROM Account LIMIT 1];
Test.startTest();
System.schedule('TEST_MySchedulable', '0 0 * * * ?', new MySchedulable(a));
Test.stopTest();
a = [SELECT Id, Description, (SELECT Id, Description FROM Contacts) FROM Account WHERE Id = :a.Id];
System.assertEquals('Contacted the customer', a.Description, 'found correct description');
for (Contact c : a.Contacts) {
System.assertEquals('Account has been contacted', c.Description, 'found correct Contact description');
}
}
The second assertion will fail, because the batch class ContactsUpdateBatch
, fired from within the asynchronous MySchedulable
, will not execute during test context - even though the first layer, MySchedulable.execute()
, is called at Test.stopTest()
.
The same pattern applies to other multi-layer asynchronous code, including @future
methods and Queueables.
There's no work-around to allow multi-level asynchronous code to execute in test context. Instead, the tests must be constructed to validate functionality without requiring this, by decomposing the tests to validate smaller units and/or using techniques like dependency injection to validate the connections between different asynchronous code units.
The example above can be effectively tested by decomposition: we can write separate unit tests against the Schedulable and the Batchable to validate their operation. The Schedulable test would validate the update to the Account and that a batch has been enqueued; the Batch test would validate the associated Contact updates.
Batch Class Execution
Unit test context permits only one batch execution (call to execute()
) to occur in a single unit test. While in most cases your unit tests would not insert more than one batch's worth of test data, it is possible to do so. This will result in an exception being thrown. Your unit test needs to guarantee that only one batch executes, either by controlling the batch size of the test data set or both.
Batches that execute across metadata objects, such as User
, are especially vulnerable to this challenge. While unit tests for these Batch classes may succeed in developer orgs or scratch orgs that have tiny record sets, they will fail when deployed to a larger production org. In many cases, these batches need at least a light dependency injection strategy to allow the unit test to control the queries executed by start()
.
For example, the query might be exposed in a @TestVisible
instance variable to allow a unit test to inject a more restricted query, or adding an Id
set to restrict the query results.
Resources
Trailhead Modules
Asynchronous Apex
- Includes modules on Future, Batch, Queueable, and Scheduled Apex, each of which describes testing approaches.
While multilayer asynchronous execution can't be asserted, the debug log may show that the child layer does execute leading to developer consternation.
– cropredy
6 hours ago
add a comment |
Asynchronous Apex includes all of the methods for executing code on the Salesforce platform outside a synchronous transaction, including:
@future
methods.- Batch Apex
- Queueable Apex
- Schedulable Apex
Because these constructs are by nature asynchronous, do not come with an SLA, and can be executed by Salesforce based upon overall system load and other considerations, we cannot typically guarantee exactly when they will be executed. This requires some changes to how we build and structure unit tests for code that is built within or makes use of any of the four asynchronous code types mentioned above.
Use of Test.startTest()
and Test.stopTest()
Because of the way Asynchronous Apex works, any asynchronous code - a future method is a useful example - will not be executed during the confines of an Apex unit test unless we take specific action. A unit test forms a single transaction, and asynchronous code enqueued within that transaction cannot be executed until the transaction commits successfully.
For this reason, Salesforce has provided a framework to force asynchronous code to execute synchronously for testing:
We enclose our test code between Test.startTest()
and Test.stopTest()
. The system collects all asynchronous calls made after startTest()
. When stopTest()
is executed, these collected asynchronous processes are then run synchronously and complete before control returns to our code.
Following Test.stopTest()
, our code can evaluate the results of the executed asynchronous code and make assertions to validate its behavior.
Test.startTest();
AsyncUtil.executeFutureMethod();
Test.stopTest();
System.assertEquals(expected, actualChangesInAsync);
Nested Asynchronous Code
The collection and synchronous execution of Asynchronous Apex applies only between Test.startTest()
and Test.stopTest()
. Any further asynchronous code that's enqueued by the asynchronous operations that are executed at Test.stopTest()
is not executed synchronously in the context of the unit test. For example, if we're working with the following code:
public class MySchedulable implements Schedulable {
private Account a;
public MySchedulable(Account a) {
this.a = a;
}
public void execute(SchedulableContext sc) {
a.Description = 'Contacted the customer');
update a;
Database.executeBatch(new ContactsUpdaterBatch(a), 200);
}
}
public class ContactsUpdaterBatch implements Database.Batchable<sObject> {
// ContactsUpdaterBatch updates the Description field on Contact (elided for brevity).
}
A unit test structured like this will not work:
@isTest
public static void updating_accounts_updates_contacts() {
Account a = [SELECT Id FROM Account LIMIT 1];
Test.startTest();
System.schedule('TEST_MySchedulable', '0 0 * * * ?', new MySchedulable(a));
Test.stopTest();
a = [SELECT Id, Description, (SELECT Id, Description FROM Contacts) FROM Account WHERE Id = :a.Id];
System.assertEquals('Contacted the customer', a.Description, 'found correct description');
for (Contact c : a.Contacts) {
System.assertEquals('Account has been contacted', c.Description, 'found correct Contact description');
}
}
The second assertion will fail, because the batch class ContactsUpdateBatch
, fired from within the asynchronous MySchedulable
, will not execute during test context - even though the first layer, MySchedulable.execute()
, is called at Test.stopTest()
.
The same pattern applies to other multi-layer asynchronous code, including @future
methods and Queueables.
There's no work-around to allow multi-level asynchronous code to execute in test context. Instead, the tests must be constructed to validate functionality without requiring this, by decomposing the tests to validate smaller units and/or using techniques like dependency injection to validate the connections between different asynchronous code units.
The example above can be effectively tested by decomposition: we can write separate unit tests against the Schedulable and the Batchable to validate their operation. The Schedulable test would validate the update to the Account and that a batch has been enqueued; the Batch test would validate the associated Contact updates.
Batch Class Execution
Unit test context permits only one batch execution (call to execute()
) to occur in a single unit test. While in most cases your unit tests would not insert more than one batch's worth of test data, it is possible to do so. This will result in an exception being thrown. Your unit test needs to guarantee that only one batch executes, either by controlling the batch size of the test data set or both.
Batches that execute across metadata objects, such as User
, are especially vulnerable to this challenge. While unit tests for these Batch classes may succeed in developer orgs or scratch orgs that have tiny record sets, they will fail when deployed to a larger production org. In many cases, these batches need at least a light dependency injection strategy to allow the unit test to control the queries executed by start()
.
For example, the query might be exposed in a @TestVisible
instance variable to allow a unit test to inject a more restricted query, or adding an Id
set to restrict the query results.
Resources
Trailhead Modules
Asynchronous Apex
- Includes modules on Future, Batch, Queueable, and Scheduled Apex, each of which describes testing approaches.
Asynchronous Apex includes all of the methods for executing code on the Salesforce platform outside a synchronous transaction, including:
@future
methods.- Batch Apex
- Queueable Apex
- Schedulable Apex
Because these constructs are by nature asynchronous, do not come with an SLA, and can be executed by Salesforce based upon overall system load and other considerations, we cannot typically guarantee exactly when they will be executed. This requires some changes to how we build and structure unit tests for code that is built within or makes use of any of the four asynchronous code types mentioned above.
Use of Test.startTest()
and Test.stopTest()
Because of the way Asynchronous Apex works, any asynchronous code - a future method is a useful example - will not be executed during the confines of an Apex unit test unless we take specific action. A unit test forms a single transaction, and asynchronous code enqueued within that transaction cannot be executed until the transaction commits successfully.
For this reason, Salesforce has provided a framework to force asynchronous code to execute synchronously for testing:
We enclose our test code between Test.startTest()
and Test.stopTest()
. The system collects all asynchronous calls made after startTest()
. When stopTest()
is executed, these collected asynchronous processes are then run synchronously and complete before control returns to our code.
Following Test.stopTest()
, our code can evaluate the results of the executed asynchronous code and make assertions to validate its behavior.
Test.startTest();
AsyncUtil.executeFutureMethod();
Test.stopTest();
System.assertEquals(expected, actualChangesInAsync);
Nested Asynchronous Code
The collection and synchronous execution of Asynchronous Apex applies only between Test.startTest()
and Test.stopTest()
. Any further asynchronous code that's enqueued by the asynchronous operations that are executed at Test.stopTest()
is not executed synchronously in the context of the unit test. For example, if we're working with the following code:
public class MySchedulable implements Schedulable {
private Account a;
public MySchedulable(Account a) {
this.a = a;
}
public void execute(SchedulableContext sc) {
a.Description = 'Contacted the customer');
update a;
Database.executeBatch(new ContactsUpdaterBatch(a), 200);
}
}
public class ContactsUpdaterBatch implements Database.Batchable<sObject> {
// ContactsUpdaterBatch updates the Description field on Contact (elided for brevity).
}
A unit test structured like this will not work:
@isTest
public static void updating_accounts_updates_contacts() {
Account a = [SELECT Id FROM Account LIMIT 1];
Test.startTest();
System.schedule('TEST_MySchedulable', '0 0 * * * ?', new MySchedulable(a));
Test.stopTest();
a = [SELECT Id, Description, (SELECT Id, Description FROM Contacts) FROM Account WHERE Id = :a.Id];
System.assertEquals('Contacted the customer', a.Description, 'found correct description');
for (Contact c : a.Contacts) {
System.assertEquals('Account has been contacted', c.Description, 'found correct Contact description');
}
}
The second assertion will fail, because the batch class ContactsUpdateBatch
, fired from within the asynchronous MySchedulable
, will not execute during test context - even though the first layer, MySchedulable.execute()
, is called at Test.stopTest()
.
The same pattern applies to other multi-layer asynchronous code, including @future
methods and Queueables.
There's no work-around to allow multi-level asynchronous code to execute in test context. Instead, the tests must be constructed to validate functionality without requiring this, by decomposing the tests to validate smaller units and/or using techniques like dependency injection to validate the connections between different asynchronous code units.
The example above can be effectively tested by decomposition: we can write separate unit tests against the Schedulable and the Batchable to validate their operation. The Schedulable test would validate the update to the Account and that a batch has been enqueued; the Batch test would validate the associated Contact updates.
Batch Class Execution
Unit test context permits only one batch execution (call to execute()
) to occur in a single unit test. While in most cases your unit tests would not insert more than one batch's worth of test data, it is possible to do so. This will result in an exception being thrown. Your unit test needs to guarantee that only one batch executes, either by controlling the batch size of the test data set or both.
Batches that execute across metadata objects, such as User
, are especially vulnerable to this challenge. While unit tests for these Batch classes may succeed in developer orgs or scratch orgs that have tiny record sets, they will fail when deployed to a larger production org. In many cases, these batches need at least a light dependency injection strategy to allow the unit test to control the queries executed by start()
.
For example, the query might be exposed in a @TestVisible
instance variable to allow a unit test to inject a more restricted query, or adding an Id
set to restrict the query results.
Resources
Trailhead Modules
Asynchronous Apex
- Includes modules on Future, Batch, Queueable, and Scheduled Apex, each of which describes testing approaches.
answered 16 hours ago
community wiki
David Reed
While multilayer asynchronous execution can't be asserted, the debug log may show that the child layer does execute leading to developer consternation.
– cropredy
6 hours ago
add a comment |
While multilayer asynchronous execution can't be asserted, the debug log may show that the child layer does execute leading to developer consternation.
– cropredy
6 hours ago
While multilayer asynchronous execution can't be asserted, the debug log may show that the child layer does execute leading to developer consternation.
– cropredy
6 hours ago
While multilayer asynchronous execution can't be asserted, the debug log may show that the child layer does execute leading to developer consternation.
– cropredy
6 hours ago
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f244791%2fhow-do-i-test-asynchronous-apex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown