+ 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))
Chrsity GrandeChrsity Grande
Very nice article you can checkout Warage Mod APK (https://medium.com/@sellygrande4/warage-mod-apk-v0-5-latest-version-free-download-7bb4c38fd0c4)  and Aka Seka Mod Apk (https://happymod19.blogspot.com/2020/03/aka-seka-mod-apk.html) Download also watchout the best site to download modded apks Happy mod (https://happymod19.blogspot.com/) apk and also checkout best site to watch anime cartoon movies for free Deadtoonsindia (https://hindimeintoonshd.blogspot.com/p/dead-toons-india.html) or Dead Toons India (https://hindimeintoonshd.blogspot.com/p/dead-toons-india.html) / Hindi Me Toon (https://hindimeintoonshd.blogspot.com/)
Chrsity GrandeChrsity Grande
Are you looking here and there for best modded website to download premium apk then you need to visit happymod.
Kinemaster Pro (https://happymod19.blogspot.com/2019/06/Download-KineMaster-Mod-APK-For-Android.html) is the best video editing apk which is used by most of the people.
DeadToonsIndia (https://hindimeintoonshd.blogspot.com/) is the best anime website.
Dead Toons India (https://medium.com/@kinemasterpromodapk/deadtoons-india-dead-toons-india-c56de9bbc399)
Sunny PaharSunny Pahar
Amazing Article! Thanks a Lot ...also I Love Watching Hindi Me Toons (https://tvtoonsindia.com/hindi-me-toons/)
Filmy ManFilmy Man
Wow nice article btw check out how to download GTA Vice City Apk (https://droidgamer.net/android/gta-vice-city-stories-for-android)
Also, check Portal Roms (https://www.portalroms.cc/).cc and Portal roms (https://portalroms.org/).org
Hindi toonsHindi toons
Very nice article. You can check out all famous series and cartoons seasons are on ( https://www.hinditoonshub.com/ (https://www.hinditoonshub.com/" target="_blank) ) available.