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
Sunay - KVP Bus SolnsSunay - KVP Bus Solns 

Help on the below Test Class to write test method

Hi,

 

Can anybody help me on writing the test method for the below Apex class:

 

global class scheduler implements Schedulable{
global void execute(SchedulableContext SC) {

List<Survey__c> slist=[Select id ,Email_Template_Id__c,Type_of_Survey__c,Number_of_Days_for_Mail__c from Survey__c ];
List<Visit_Details__c> tlist;
List<Visit_Details__c> tlistupdate= new List<Visit_Details__c>();
List<Booking_Detail__c> blist;
List<Booking_Detail__c> blistupdate= new List<Booking_Detail__c>();

EmailTemplate e;
List< Messaging.SendEmailResult> semr;
String url;
String mail_html;
Integer sendcount=0;
Datetime cdate;
String[] toAddresses = new String[]{} ;
for(integer i=0;i<slist.size();i++)
{ if(sendcount>=10)
break;

try
{
e = [select Id,Name,Subject,body ,htmlvalue from EmailTemplate where Id=:slist[i].Email_Template_Id__c];
if((e.htmlvalue=='')||(e.htmlvalue==null))
mail_html= e.body;
else
mail_html=e.htmlvalue;
}
catch(Exception ee)
{

}



if(slist[i].Type_of_Survey__c=='Post Visit')
{

try
{
cdate =System.now().addDays(-(Integer.valueOf(slist[i].Number_of_Days_for_Mail__c)));
}
catch(Exception err)
{
}
tlist= [Select id ,Survey_Status__c,Check_In_Date__c,Guest_Profile_Email_Id__c,createdDate from Visit_Details__c where Survey_Status__c!=:'sent' AND createddate >: cdate ];
for(integer email=0;email<tlist.size();email++)
{ if(sendcount<10)
sendcount++;
else
break;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

toAddresses.clear();

toAddresses.add(tlist[email].Guest_Profile_Email_Id__c);
mail.setToAddresses(toAddresses );

mail.setSubject(e.Subject);

url='<a href=\"http://idsnext-developer-edition.na12.force.com/TakeSurvey?id='+slist[i].id+'&caId='+tlist[email].id+'\">click here to make your survey</a>';
try
{
mail_html=mail_html.replace('{!Survey__c.URL__c}',url);


}
catch(Exception html)
{
mail_html=' ';
}
mail.setHTMLBody(mail_html );
try
{
semr= Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
catch(Exception toomanymails)
{

}
if(semr[0].isSuccess()==true)
{
tlist[email].Survey_Status__c='Sent';
tlistupdate.add(tlist[email]);
}
else
{
tlist[email].Survey_Status__c='Not Sent';
tlistupdate.add(tlist[email]);
}
url='';
mail_html='';
update tlistupdate;
}
}


if(slist[i].Type_of_Survey__c=='Pre Visit')
{


try
{
cdate =System.now().addDays(-(Integer.valueOf(slist[i].Number_of_Days_for_Mail__c)));
}
catch(Exception err)
{
}
blist= [Select id ,Survey_Status__c,Arrival_Date__c,Contact_Email__c,createdDate from Booking_Detail__c where Survey_Status__c!=:'Sent' AND createddate >: cdate ];
for(integer email=0;email<blist.size();email++)
{ if(sendcount<10)
sendcount++;
else
break;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

toAddresses.clear();

toAddresses.add(blist[email].Contact_Email__c);
mail.setToAddresses(toAddresses );

mail.setSubject(e.Subject);

url='<a href=\"http://idsnext-developer-edition.na12.force.com/TakeSurvey?id='+slist[i].id+'&caId='+blist[email].id+'\">click here to make your survey</a>';
try
{
mail_html=mail_html.replace('{!Survey__c.URL__c}',url);

}
catch(Exception html)
{
mail_html=' ';
}

mail.setHTMLBody(mail_html );
try
{
semr= Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
}
catch(Exception toomanymails)
{

}
if(semr[0].isSuccess()==true)
{
blist[email].Survey_Status__c='Sent';
blistupdate.add(blist[email]);
}
else
{
blist[email].Survey_Status__c='Not Sent';
blistupdate.add(blist[email]);
}
url='';
mail_html='';
update blistupdate;
}
}
}

}

}

 

I tried so many possible ways...but not able to cover 1% of code. Will be grateful for any help.

 

 

soofsoof

I'm assuming that the issue here is related to writing test code for 'schedulable' class...

 

Please review this "Testing the Apex Scheduler" section of the following page in apex docs:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

 

Also, if you take a look at the "Apex Scheduler Best Practices and Limits" section on the same page, you'll see that salesforce recommends not having a lot of code in the schedulable class:

Though it's possible to do additional processing in the execute method, we recommend that all processing take place in a separate class.

 

If you do that, you can forget about writing code for the schedulable class, and just write test code for the 'separate class'.

 

Hope this helps.

kiranmutturukiranmutturu

try some thing like this

@istest

class TestClass {

static testmethod void test() {
Test.startTest();
String jobId = System.schedule('testBasicScheduledApex',
cron expression here,
new scheduler());

Test.stopTest();

}
}