Test Steps
Creating tests for your code creations is always important. How else to make sure your code is working properly at every step. You can learn more about tests here. For now we will just create some example classes to test your code.
How to test Step
-
CountEmployeesStep
As a first example let’s test our CountEmployeesStep as it present interesting case of using ModificationContext for purposes of tests. Test setup method usually provides records that will be used for testing, here we test step that changes NumberOfEmployees on an Account record based on how many Contact records there are with a lookup relationship with that existing record. So the test strategy is pretty simple: create Account → insert Account → create Contact records with lookup to Account, nonetheless as the test goes we can’t always make sure that update will be made unless we check the results afterwards.
For the first method we will be just testing Step with database context so we insert contacts and check if the trigger connected to this functionality works properly. To run this test both trigger and step group need to be activated.
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}
Second method just tests execution of Step with modification context, which does not need active triggers, step groups not even step. Thanks to BreezzApi we can create a modification context which is a result of the execute method. After that we just build Step using the build method and pass our logic which was provided in the CountEmployeesStep class using the addstep method.
Take notice of skipModificationContextSave() method which is important here because even though Breezz Builder creates a way to execute step logic from code, this also clears ModificationContext after saving. Without committing those changes to the database we wouldn’t know if there were any changes. Lastly, to get our Account record we use getRecordToUpdate as a way to extract it from context. That’s because in our CountEmployeesStep class we used addModificationToUpdate.
-
LeadConverter
Every Step is different and sometimes to fully test your functionality you have to seriously flex your brain muscle. Breezz allows you to take advantage of BreezzApi that seriously helps with every step of development especially in tests. For the second example we will test the LeadConverter class. Premise is simple enough if the class receives lead record with status defined as conversion point using Database.convertLead method record is erased and translated to Account, Opportunity and Contact record. Every of these new records have information based on the initial Lead record. So to test our converter we just need to insert or update a lead with initial conditions met and check for database results.
1@IsTest
2private class LeadConverterTest {
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_ProvidedLeadRecord_expect_ConvertLead() {
17 // create new lead, update status to one that has a isConverted value set to true
18 Lead[] ld = new Lead[] {new Lead(LastName = 'LastName', MobilePhone = '1234', Company = 'Company')};
19 insert ld;
20
21
22 Test.startTest();
23 ld[0].Status = 'Closed - Converted';
24 update ld;
25 forvendi.BreezzApi.STEPS.build()
26 .addStep(new LeadConverter())
27 .execute(ld);
28 Test.stopTest();
29
30
31 // check database for result which here is account,contact and opportunity
32 // records created
33 Opportunity[] opps = forvendi.BreezzApi.TESTS.loadAllRecords(Opportunity.getSObjectType());
34 Account[] accs = forvendi.BreezzApi.TESTS.loadAllRecords(Account.getSObjectType());
35 forvendi.BreezzAPI.TESTS.assertErrorLogs();
36 Assert.isNotNull(ld);
37 Assert.areEqual(1, opps.size());
38 Assert.areEqual(1, accs.size());
39 }
40}
If execution of logic inside your class is affecting other objects but you don’t have a way to directly pull them by ID with BreezzApi.TESTS.loadRecordById you can simply use another method provided with Breezz Api which is leadAllRecords. This method allows you to gather all records of a certain ObjectType from the apex test framework.