+ Start a Discussion
dinesh abcdinesh abc 

Iam facing problem with test class

User-added image
laxmi narayan 11laxmi narayan 11
hi Dinesh,

what type of error it is showing?

Thanks
LN
dinesh abcdinesh abc
System.EmailException: SendEmail failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Add a recipient to send an email.: []


batchclass:
global class Birthdayremainder implements Database.Batchable<contact>,Database.Stateful {
     global list<string> emailids =new list<string>();
     global Iterable<contact> start(Database.BatchableContext bc){
     list<contact> conlist=[select lastname,Email from contact where Birthdate =:system.today()+7];
     return conlist;
    }
global  void execute(Database.BatchableContext bc, List<contact> lst)
      {
        for(contact con: lst)
        {
            emailids.add(con.email);
        }
          
      }
   global  void finish(Database.BatchableContext bc) 
   {
       list<Messaging.SingleEmailMessage> mail = new list<Messaging.SingleEmailMessage>();
       Messaging.SingleEmailMessage maileach = new Messaging.SingleEmailMessage();
          maileach.settoaddresses(emailids);
          maileach.setsubject('birthday wishes');
          maileach.setplaintextbody('happy birthday ');
          mail.add(maileach);
       Messaging.SendEmailResult[]  result=Messaging.sendEmail( mail);
   }
}

schedule class:
____________________________________________________________________
global class Birthdayschedule implements Schedulable{

global   void execute(SchedulableContext dc){
   
      Birthdayremainder bdr=new Birthdayremainder ();
      Database.executeBatch(bdr,20);
    }
}
Ramesh DepaiahRamesh Depaiah
@Dinesh 
Your testclass is failing because you are looking for contacts which birthdate equals system.today()+7 but in your test class you are creating system.today()+6 So which will return 0 contacts. Change it to 7 and try like below
 
@isTest
public class BirthdayremainderTest {
    public static testmethod void birthdayTest()
    {
        Test.startTest();
        contact con=new contact();
        con.LastName='tam';
        con.Email='ramesh@test.com';
        con.Birthdate=system.today()+7;
        insert con;
        
        Birthdayschedule bs= new Birthdayschedule();
        string sch='0 26 15 ? * * *';
        system.schedule('RemindBirthday', sch, bs);
        test.stopTest();
    }
}
Thanks
​​​​​​​Ramesh