+ Start a Discussion
Michael MMichael M 

Test class for apex schedulable class__

Hello, Would someone be able to assist in writing a test class for the following apex class:
global class purgeFiles implements Schedulable{
    
    global void execute(SchedulableContext SC) {
        
    List<ContentDocumentLink > docs;
        docs = [SELECT Id, Linkedentity.name, Linkedentity.id,ContentDocument.CreatedDate  
                FROM contentDocumentLink 
                WHERE Linkedentityid 
                IN (SELECT Id FROM lead where createddate < Last_N_Days:7 and isconverted = false) ];  
                //may want to change to include ALL leads
                //AND ContentDocument.CreatedDate < Last_N_Days:7];
                if(!docs.isEmpty())
            delete docs;
    }
}

Thank you very much
Best Answer chosen by Michael M
AnudeepAnudeep (Salesforce Developers) 
Hi Michael, 

You can start with something like this

@isTest 
public class purgeFilesTest 
{
    static testMethod void testMethod1() 
    {
        List<contentDocumentLink> lstCon = new List<contentDocumentLink>();
         
         Account acc = new Account(name='Test Account');
         insert acc;

         ContentNote n = new ContentNote();
         n.title = 'My Note from Apex';
         insert n;

        for(Integer i=0 ;i <200;i++)
        {
            
            ContentDocumentLink contentlink=new ContentDocumentLink();
            contentlink.LinkedEntityId = acc.id;
            contentlink.ShareType= 'V';
            contentlink.ContentDocumentId=n.id;
            contentlink.Visibility = 'AllUsers';
            lstCon.add(contentlink);
        }
        
         insert lstCon;
       
        Test.startTest();

            purgeFiles obj = new purgeFiles();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();

       // After the testing stops, Please assert if records are updated properly
    }
}

You can learn more about testing batch apex here: 

https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_batch

Anudeep

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Michael, 

You can start with something like this

@isTest 
public class purgeFilesTest 
{
    static testMethod void testMethod1() 
    {
        List<contentDocumentLink> lstCon = new List<contentDocumentLink>();
         
         Account acc = new Account(name='Test Account');
         insert acc;

         ContentNote n = new ContentNote();
         n.title = 'My Note from Apex';
         insert n;

        for(Integer i=0 ;i <200;i++)
        {
            
            ContentDocumentLink contentlink=new ContentDocumentLink();
            contentlink.LinkedEntityId = acc.id;
            contentlink.ShareType= 'V';
            contentlink.ContentDocumentId=n.id;
            contentlink.Visibility = 'AllUsers';
            lstCon.add(contentlink);
        }
        
         insert lstCon;
       
        Test.startTest();

            purgeFiles obj = new purgeFiles();
            DataBase.executeBatch(obj); 
            
        Test.stopTest();

       // After the testing stops, Please assert if records are updated properly
    }
}

You can learn more about testing batch apex here: 

https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_batch

Anudeep
This was selected as the best answer
IPL TeamsIPL Teams
I had the same query. Thank you very much for your answer and question.
IPL Teams (https://www.iplteams.xyz/)
IPL 2020 Teams and Players (https://www.iplteams.xyz/)
IPL Players and Team List (https://www.iplteams.xyz/)
george william 8george william 8
<a href="https://sites.google.com/view/wwwnortoncomsetup-nortonsetup/home">Enter Your Product key Here | Norton Setup | Norton Activation | Norton install</a>
george william 8george william 8
Enter Your Product key Here | Norton Setup | Norton Activation | Norton install (https://sites.google.com/view/wwwnortoncomsetup-nortonsetup/home) (https://sites.google.com/view/wwwnortoncomsetup-nortonsetup/home))