How to test Async Step by forcing Sync execution
Below code tests Async Step by forcing Sync execution.
1@IsTest
2private class CountEmployeesStepTest {
3
4
5 @TestSetup
6 static void testSetup() {
7 // initializing Breezz setup,
8 // you have to provide forvendi.BreezzPlugins.BaseApexPlugin implementation
9 // if you would like to
10 // use public classes like UppercaseNameFieldStep in Breezz
11 forvendi.BreezzApi.TESTS.init('BreezzPlugin');
12 }
13
14
15 @IsTest
16 static void when_CountEveryContactAsEmployee_expect_ContactAddedToAccount() {
17 // create basic account and at least 2 contacts, insert and pull with TESTS.loadRecordById
18 // to check updated account values
19 Account acc = new Account(Name = 'Employer');
20 insert acc;
21 Contact[] cnts= new Contact[]{
22 new Contact(LastName = 'Johnson', AccountId = acc.Id),
23 new Contact(LastName = 'Jordan',AccountId=acc.Id)};
24 Test.startTest();
25 insert cnts;
26 Test.stopTest();
27
28
29 Account account = (Account) forvendi.BreezzAPI.TESTS.loadRecordById(acc.Id);
30 forvendi.BreezzAPI.TESTS.assertErrorLogs();
31 Assert.IsNotNull(account);
32 Assert.IsNotNull(cnts);
33 Assert.areEqual(2, account.NumberOfEmployees);
34 }
35
36
37 @IsTest
38 static void when_ExecutedCountEmployeesStep_expect_CountEveryContactAsEmployee() {
39 // create basic account and at least 2 contacts, insert all.
40 // execute STEP_FUNCTION.build() to test class logic
41 Account acc = new Account(Name = 'Employer');
42 insert acc;
43 Contact[] cnts= new Contact[] {
44 new Contact(LastName = 'Johnson',AccountId=acc.Id),
45 new Contact(LastName = 'Jordan', AccountId=acc.Id)};
46 insert cnts;
47
48
49 Test.startTest();
50 // use skipModificationContextSave() to skip memory clear in modificationContext
51 forvendi.ModificationContext ctx = forvendi.BreezzApi.STEPS.build()
52 .skipModificationContextSave()
53 .addStep(new CountEmployeesStep())
54 .execute(cnts);
55 Test.stopTest();
56
57 forvendi.BreezzAPI.TESTS.assertErrorLogs();
58 Assert.IsNotNull(ctx.getRecordToUpdate(acc.Id));
59 Assert.areEqual(2, ctx.getRecordToUpdate(acc.Id).get('NumberOfEmployees'));
60 }
61}