Apex
How to get test coverage of co...
Sample Use case
 suppose you've a requirement from business to create a flow action that get's attachments using get attachments docid\ eb26zx4dgrfadxfzqvj35 from a record and creates sharing links using create links docid\ sreco10qovwtthndtraua for only the files with type pdf inputs the inputs to the action would be recordid of the record to be processed ouputs outputs of this custom action would be what create links docid\ sreco10qovwtthndtraua returns why do we need a custom mock class as explained in the about clientfactory docid\ kjscoqk3cmnoqi130pvpy article, clientfactory returns a default mocked implementation of client methods, these default methods may not have return values specific to our use case and hence for more control, you can define a custommock class which extends clientfactory and inject it as a dependency while getting a clientfactory instance below is a custom mock implementation and how to use it for our use case defined earlier apex custom mock implementation example public class custommock extends cldfs clientfactory{ public override list\<list\<cldfs attachment>> listattachments(list\<string> parentids,string runas) { list\<list\<cldfs attachment>> result = new list\<list\<cldfs attachment>>(); list\<cldfs attachment> attlist = new list\<cldfs attachment>(); cldfs attachment file = new cldfs attachment(); file id = 'testid 1'; file resource = new cldfs resource( 'testid 1', 'file', 'test file pdf', 'library', 'driveid 1' ); cldfs attachment folderattachment = new cldfs attachment(); folderattachment id = 'testfolderid 2'; folderattachment resource = new cldfs resource( 'testfolderid 2', 'folder', 'folder name 2', 'library', 'driveid 1' ); attlist add(file); attlist add(folderattachment); result add(attlist); return result; } public override list\<cldfs link> createlinks(list\<cldfs types createlinkinput> linkinputs, string library, string runas) { list\<cldfs link> links = new list\<cldfs link>(); for (integer i = 0; i < linkinputs size(); i++) { cldfs link l = new cldfs link(); l id = 'testid ' + string valueof(i); l url = 'http //test url/' + string valueof(i); links add(l); } return links; } } apex custom action generate pdf links from attachments public with sharing class generatepdflinksaction { public static list\<cldfs link> generatepdflinksaction(id recordid) { cldfs clientfactory client = cldfs clientfactory getinstance(); list\<list\<cldfs attachment>> attachments = client listattachments(new list\<id>{recordid}, 'integrationuser'); list\<cldfs attachment> attachment = attachments\[0]; list\<cldfs types createlinkinput> linkinputs = new list\<cldfs types createlinkinput>(); string library = attachment\[0] resource library; for (cldfs attachment attach attachment) { cldfs resource resource = attach resource; if (resource name endswith(' pdf')) { linkinputs add( new cldfs types createlinkinput( resource id, resource type, recordid, null, resource driveid ) ); } } return client createlinks( linkinputs, library, 'integrationuser' ); } } using the custom mock in tests to use your mock in the test environment, modify the action like this cldfs clientfactory client = cldfs clientfactory getinstance(new custommock()); this ensures the custom mock is used when tests are executed, simulating realistic responses for both get attachments docid\ eb26zx4dgrfadxfzqvj35 and create links docid\ sreco10qovwtthndtraua apex test class for generatepdflinksaction @istest public with sharing class testgeneratepdflinksaction { @testsetup public static void setupdata(){ account acc = new account(name = 'test account'); insert acc; } @istest public static void testgeneratepdflinksaction() { account account = \[select id from account limit 1]; test starttest(); list\<cldfs link> links= generatepdflinksaction generatepdflinksaction(account id); test stoptest(); assert isnotnull(links); } }