function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Angadi VCAngadi VC 

Test class for Schedulable class ,code coverage error in production

Hi,
I have a schedule class which inserts a chatter post on user birthyday.
There is test class for same which give 100% coverage in sandbox,but while deploying it gives code coverage error with 73% coverage.
Kindly help me with this,
Thanks in advance.
Schedule class:

global class AnniversayBirthydayChatter implements Schedulable{
 

global void execute(SchedulableContext sc) {

 List<user> lstbirthday=[Select id,name,UserRole.name,User_State__c from user where IS_birthday_today__c=true and isactive=true];// query on birthday of users
 
 List<user> lstanniversary=[Select id,name,UserRole.name,User_State__c from user where IS_Anniversary_today__c=true and isactive=true];//query of anniversary of users
     
     list<FeedItem> postlist1= new list<FeedItem>();
     list<FeedItem> postlist2= new list<FeedItem>();
     
       for(User u:lstbirthday){
        FeedItem post = new FeedItem(); // chatter instance creation
        
        post.ParentId = u.id;
        
         post.Body = 'Join together in wishing a very Happy Birthday to '+'-'+u.name +'-'+u.UserRole.name+'-'+u.User_State__c ;
         postlist1.add(post);
           
       }
       if(postlist1.size()>0 && postlist1!=null){
          insert postlist1;
       }
         for(User u:lstanniversary){
            FeedItem post = new FeedItem();// chatter instance creation
            
            post.ParentId = u.id;
            
             post.Body = 'Ashirvad Family wishes you both a very happy anniversary. Wish you both lot of happiness & good health'+'-'+'Mr & Mrs.'+'-'+u.name+'-' +u.UserRole.name+'-'+u.User_State__c ;
             postlist2.add(post);
           
       }
           if(postlist2.size()>0 && postlist2!=null){ //check for list size
              insert postlist2;
           }

}
}

Test class:
 @isTest
   public class Test_AnniversayBirthydayChatter  {
        @isTest static void Chattertestmethod(){
        Test.startTest();
 AnniversayBirthydayChatter abc=new AnniversayBirthydayChatter();
String sch = '0 00 8 * * ?'; //schedule interval time
        system.schedule('AnniversayBirthydayChatter',sch , abc); //system method to schedule apex class
         Test.stopTest();
        }
    }
Ajay K DubediAjay K Dubedi
Hi Angadi,
    Use the below code it will help you:    
    @isTest
    public class Test_AnniversayBirthydayChatter  {
        @isTest static void Chattertestmethod(){
        Test.startTest();
        User newUser = new User();
        newUser.LastName ='Abc';
        newUser.Alias = 'Rana';
        newUser.LocaleSidKey = 'en_US';
        newUser.LanguageLocaleKey = 'en_US';
        newUser.UserPermissionsOfflineUser = true;
        newUser.UserPermissionsMarketingUser = true;
        newUser.Email = 'abc@gmail.com';
        newUser.Username = 'xyz@gmail.com';
        newUser.TimeZoneSidKey='GMT';
        newUser.ProfileId = UserInfo.getProfileId();
        newUser.EmailEncodingKey = 'UTF-8';
        newUser.DigestFrequency = 'D';
        newUser.DefaultGroupNotificationFrequency = 'D';
        newUser.IS_birthday_today__c = true;
        newUser.isactive = true;
        insert newUser;
        User newUser1 = new User();
        newUser1.LastName ='xyz';
        newUser1.Alias = 'Rana';
        newUser1.LocaleSidKey = 'en_US';
        newUser1.LanguageLocaleKey = 'en_US';
        newUser1.UserPermissionsOfflineUser = true;
        newUser1.UserPermissionsMarketingUser = true;
        newUser1.Email = 'abc@gmail.com';
        newUser1.Username = 'xyz@gmail.com';
        newUser1.TimeZoneSidKey='GMT';
        newUser1.ProfileId = UserInfo.getProfileId();
        newUser1.EmailEncodingKey = 'UTF-8';
        newUser1.DigestFrequency = 'D';
        newUser1.DefaultGroupNotificationFrequency = 'D';
        newUser1.IS_Anniversary_today__c = true;
        newUser1.isactive = true;
        insert newUser1;
        
        AnniversayBirthydayChatter userClassObj = new AnniversayBirthydayChatter();
        String schCrone = '0 00 8 * * ?'; 
        System.schedule('AnniversayBirthydayChatter',schCrone, userClassObj);
         Test.stopTest();
        }
    }
        

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi