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
troungertrounger 

Regarding test class for scheduler apex class

hi guys  i am writing test class for scheduled apex class i wrote test class like regular apex class this but its showing 0% coverage after that i learned i have to write diffrently just like in this meterial http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

 

But the i am allways writing scheduled apex class and  schedule those classes using schedule  button using develop ->apexclass->schedule button i never used cron expression and interface class so i am not getting how to write test class using above meterial anyone please help  me with this issue  here its my schedule class

 

global class shedulingaemail implements Schedulable{
   public date d2;
    global void execute(SchedulableContext ctx) {  
               d2 = system.today();         
    for(Contact c:[ select Id,Next_Due_Date__c,Next_Due_Date1__c,Personal_Email__c,acknowledgement__c from Contact] )  
    {
         if(c.Next_Due_Date1__c == d2 && c.acknowledgement__c != 'Received')
        {    
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] {c.Personal_Email__c} );
       // mail.setToAddresses(new String[] {c.Alternate_Email__c});
            mail.setReplyTo('lakshmid@bighelp.org');
            mail.setSenderDisplayName('Bighelp Notifications!');
            mail.setSubject('Renewal Request');
            mail.setBccSender(false);
            mail.setUseSignature(false);
               mail.setHtmlBody('<p> Dear <b> ' +c.Name +', </b></p>'+'<br></br>'+
                                   '<p>Bighelp For Education would like to thank you for kindly sponsoring the education of underprivileged children in India. Your support gave the hope for the bright future for the children who otherwise could not even dream about having any future! Following link has the information about your sponsoring children.</p>'+'<br></br>'+
                                   '<p>The next due date to renew your sponsorship'+c.Next_Due_Date__c+'. We request you to kindly renew your sponsorship so that we can continue supporting your sponsoring children. </p>'+'<br></br>'+
                                   '<p>If you decide to continue your sponsorship, please mail the donation for $132 ($11/month per child) to continue your sponsorship for one year. Please write check payable to "Bighelp For Education" and mail to following address.</p>'+'<br></br>'+
                                   '<p>Bighelp For Education</p>'+
                                   '<p>21C Hawthorne Village</p>'+
                                   '<p>Frankline,MA 02038, USA</p>'+'<br></br>'+
                                  '<p>Donation can also be made online through credit card or PayPal at following link.</p>'+'<br></br>'+
                                  '<p>http://www.bighelp.org/bhp/site/donatenow</p>'+'<br></br>'+
                                  '<p>Thank you so much for your continued support. We look forward to hear from you soon.</p>'+'<br></br>'+
                                  '<p>With best regards, </p>'+
                                 '<p>Bighelp For Education</p>'+
                                 '<p>www.bighelp.org</p>');
              //mail.setPlainTextBody('Hi a harsha this is sheduling mail cheking');
 // Send the email
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
            C.Next_due_date1__c = c.Next_due_date1__c + 7;
            update c;
             
    }
    }
}
}

Thanks in Advance

Best Answer chosen by Admin (Salesforce Developers) 
ManojjenaManojjena

@isTest
public class TestSchedule {
static testMethod void unitTest(){
Contact con=new Contact();
con.acknowledgement__c='Received';
con.Next_Due_Date__c=System.today();
con.Personal_Email__c='test@testmail.com';
con.Alternate_Email__c='something@email.com';
Insert con;

Contact testCon=[SELECT Id,acknowledgement__c FROM Contact WHERE Id=:con.Id LIMIT 1];
System.assertEquals(testCon.acknowledgement__c,'Received');
shedulingaemail seMail=new shedulingaemail();
seMail.d2=System.today();
SchedulableContext sc;
seMail.execute(sc);
}
}

Try the above code .Needs to give the mandatory fields in contact to insert contact .

All Answers

ManojjenaManojjena

@isTest
public class TestSchedule {
static testMethod void unitTest(){
Contact con=new Contact();
con.acknowledgement__c='Received';
con.Next_Due_Date__c=System.today();
con.Personal_Email__c='test@testmail.com';
con.Alternate_Email__c='something@email.com';
Insert con;

Contact testCon=[SELECT Id,acknowledgement__c FROM Contact WHERE Id=:con.Id LIMIT 1];
System.assertEquals(testCon.acknowledgement__c,'Received');
shedulingaemail seMail=new shedulingaemail();
seMail.d2=System.today();
SchedulableContext sc;
seMail.execute(sc);
}
}

Try the above code .Needs to give the mandatory fields in contact to insert contact .

This was selected as the best answer
troungertrounger

Thank u very much situ its not covering the entire code but its covering main things i can write remaining code

 

Thanks