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
dkgmdkgm 

TestClassProblem

This is the schedule class for which i need to write test class:

 

global class SendNotificationToOpportunityOwner Implements Schedulable{
   List<String> oppIdList=new List<String>();
   List<OpportunityLineItem> productList;
   global void execute(SchedulableContext sc){
       System.debug('*******************');
       productList=[SELECT SHRMI_End_Date__c,Id,OpportunityId FROM OpportunityLineItem WHERE SHRMI_End_Date__c != null AND OpportunityId != null ] ;
       for(OpportunityLineItem oppLineItem :productList){
          Integer numberDaysRemaining =System.Today().daysBetween(oppLineItem.SHRMI_End_Date__c);
          if(numberDaysRemaining ==60){
              oppIdList.add(oppLineItem.OpportunityId);
          }//end of If
      }//end of for loop 
      List<Opportunity> oppList=[SELECT Id,OwnerId,SHRMI_OwnerName__c,SHRMI_OwnerEmail__c from Opportunity WHERE Id IN:oppIdList];
      List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
      List<Task> taskListtoInsert=new List<Task>();
      for(Opportunity opp :oppList ){
       String oppUrl = URL.getSalesforceBaseUrl().toExternalForm() +'/' + opp.id;
       //creating new task under opportunity
       Task tsk=new Task();
          tsk.whatid=opp.Id;
          tsk.OwnerId=opp.OwnerId;
          tsk.Priority='High';
          tsk.Subject='Follow up on renewal membership';
          tsk.status='Not Started';
          tsk.ActivityDate=System.Today().addDays(60);
          taskListtoInsert.add(tsk);
       //Sending email to opportunity owner
       Messaging.SingleEmailMessage mail= new Messaging.SingleEmailMessage();
           String[] toAddresses = new String[]{opp.SHRMI_OwnerEmail__c};
           mail.setToAddresses(toAddresses);
           mail.setSubject('Licence Renewal');
           mail.setHtmlBody('Dear  '+opp.SHRMI_OwnerName__c+',<br/><br/>Please follow up with the  customer to renew the license.'+'<br/><br/><br/>'+'For details, click the link below'+'<br/>'+oppUrl+'<br/><br/>'+'Thanks,'+'<br/>'+'<b>'+'System Admin'+'</b>');
           emailList.add(mail);
      }
      try{
          Insert taskListtoInsert;
      }catch(DmlException de){System.debug(de);}
      Messaging.sendEmail(emailList);
    }//end of method
}//end of class

 

This is my test class which is covering 38% test coverage.How will i increase my test coverage???

 

 

@isTest
private class TestSendNotificationToOpportunityOwner {

static testMethod void myUnitTest() {
Test.startTest();
String testCronExp = '0 0 1-23 * * ?';
SendNotificationToOpportunityOwner enqObj = new SendNotificationToOpportunityOwner();
System.schedule('RandomizationSchedule', testCronExp, enqObj);
Test.stopTest();
}
}

 

 

 

MJ Kahn / OpFocusMJ Kahn / OpFocus

When your unit tests run, any pre-existing data (like Opportunity Line Items) isn't visible to your unit test, so when line 6 of your class goes looking for existing OLIs, it's not goingg to find any. You have to change your unit tests to create an Opportunity Line Item (and therefore, and Opportunity, and Account, and anything else that Opportunity Line Items are dependent on) before you try to run your schedulable job.

dkgmdkgm

Thanks For your kind reply.Now it is working fine.