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
nagalakshminagalakshmi 

how to get 75% test coverage

Hi, Any one please help me for getting 75% code coverage, I am getting 71% code coverage. I tried a lot but not getting.
My Class is:

}

global class scheduledmonthlyTaskJobScheduler
{

public string times{set;get;}

global void scheduledmonthlyTaskJobScheduler()
{

}
public static void start()
{
string ctname;
id jobid1;
for(Testscheduling__c ct:[select name from Testscheduling__c ])
ctname=ct.name;
system.debug(ctname+'123@@@@@@@@');
if(ctname!=null || ctname!='')
{
for(crontrigger ctr :[select id,CronExpression from crontrigger where id=:ctname])
{
if(ctr!=null)
jobid1=ctr.id;
system.debug(ctr+'########'+jobid1);
System.abortJob(jobid1);
}
}
list<Testscheduling__c>  tt= new list<testscheduling__c>([select id from testscheduling__c]);
if(tt.size()>0)
delete tt;
Schedule_Time__c stc = Schedule_Time__c.getinstance();
if(stc.Date_Time__c!=null)
 {
 string str = string.valueof(stc.Date_Time__c);
 String[] myDateOnly = str.split(' ');
 String[] strDate = myDateOnly[0].split('-');   
 String[] strtime = myDateOnly[1].split(':');  
 string Month=string.valueof(system.today().month());
 string year=string.valueof(system.today().year());
 string Day= strDate[2];
 string hrs= strtime[0];
 string  mins=strtime[1];
 string sec= strtime[2];
 Datetime dcomp=datetime.newInstance(system.today().year(),system.today().month(),Integer.valueof(strDate[2]),Integer.valueof(strtime[0]),Integer.valueof(strtime[1]),Integer.valueof(strtime[2]));
 if(system.now()<dcomp){
   
 string  times = sec+' '+Mins+' '+hrs+' '+day+' '+Month+' '+'?'+' '+year;
 monthlyTaskscheduled st= new monthlyTaskscheduled();
 string SCHEDULE_NAME = 'scheduledmonthlyTask';
 //System.schedule(SCHEDULE_NAME,times,st);
 Id jobid = System.schedule(SCHEDULE_NAME,times,st);
 Testscheduling__c t = new Testscheduling__c();
  t.name=jobid;
  insert t;
  }
 }
 }

 

My test coverage class is:

 

}

public class testscheduledmonthlyTaskJobScheduler
{
static testMethod void test()
{
scheduledmonthlyTaskJobScheduler s=new scheduledmonthlyTaskJobScheduler();
list<Schedule_Time__c> slist=new list<Schedule_Time__c>(); 
set<id> pset=new set<id>();
Testscheduling__c t1=new Testscheduling__c(name='aaa');
insert t1;
datetime d1;
monthlyTaskscheduled st= new monthlyTaskscheduled();
Testscheduling__c t = new Testscheduling__c();
for(profile p:[select id,name from profile where name='System administrator'])
pset.add(p.id);
for(Schedule_Time__c s1:[select id,date_time__c,SetupOwnerId from Schedule_Time__c where SetupOwnerId IN :pset])
{
if(s1.id!=null)
{
s1.date_time__c=system.now().adddays(1);
d1=s1.date_time__c;
slist.add(s1);
}
}
update slist;
Schedule_Time__c stc = Schedule_Time__c.getinstance();
//Datetime d2=datetime.newInstance(system.today().year(),system.today().month(),stc.date_time__C.day(),stc.date_time__c.hour(),stc.date_time__c.minute(),stc.date_time__C.second()); 
Datetime d2=datetime.newInstance(system.today().year(),system.today().month(),d1.day(),d1.hour(),d1.minute(),d1.second());
if(system.now()< d2)
{
string times=d1.second()+' '+d1.minute()+' '+d1.hour()+' '+d1.day()+' '+system.today().month()+' ? '+system.today().year();
Id jobid = System.schedule('name',times,st);
t.name=jobid;
insert t;
}
scheduledmonthlyTaskJobScheduler.start();
s.scheduledmonthlyTaskJobScheduler();
}

 

I code which is in bold is not covered. please help me for solving this.. Thanks in advance.

 

Ivar@MarelIvar@Marel

You might get around this by doing something like this. I hope this makes sense:

 

1. Add a boolean variable to your class definition:

global class scheduledmonthlyTaskJobScheduler
{
   boolean bIsTest = false;
...

 2. Then change your non covered code blocks to include the boolean as a criteria. Example:

 if(system.now()<dcomp || bIsTest ){

 3. Add a new test method that sets the boolean to true insuring that the blocks are executed. Example:

static testMethod void fillCoverage(){
  this.bIsTest = true;
  //run code
}

 This is not a conclusive solution, but I hope you get the idea and it helps.

 

Regards,

Ivar

 

Starz26Starz26

It your test class, create a record where:

 

select id,CronExpression from crontrigger where id=:ctname

 

Will return a result, that should get you into the first loop

 

For the second part:

 

Datetime dcomp=datetime.newInstance(system.today().year(),system.today().month(),Integer.valueof(strDate[2]),Integer.valueof(strtime[0]),Integer.valueof(strtime[1]),Integer.valueof(strtime[2]));
 if(system.now()<dcomp){


 

Set up the test so that dComp actually returns something > now.

 

 

 

 

pconpcon

Not to detract from the topic, but instead of setting a variable bIsTest, it's better to use the static built-in method of Test.isRunningTest()