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
sakthivelsakthivel 

Need help in creating a test case for scheduled apex

Can somebody help me creating a test case please...  it is a scheduled apex class, here is my code ..

 

global class FBR_ApexScheduleMonthClass Implements Schedulable
            {
                       global void execute(SchedulableContext sc)
                        {
                                    ValAdd();
                        }
 public  static void ValAdd()//Set<Id> InquiryId
  {  
  
List<FBR_Inquiry__c> Inquiry= [SELECT KPIInquiryContent__c,KPIInquiryContent__r.Account__c,CommodityName__c,ModifyDate__c,Status__c from FBR_Inquiry__c  ];
{
FBR_Inquiry__c FBR= Inquiry[0];
Date startDate=FBR.ModifyDate__c.toStartOfMonth();
Date endDate=FBR.ModifyDate__c.toStartOfMonth().addMonths(1);

LIST<AggregateResult> countmonth=[SELECT count(KPIInquiryContent__c) coun from FBR_Inquiry__c where KPIInquiryContent__r.Account__c=:FBR.KPIInquiryContent__r.Account__c and ModifyDate__c >= :startDate AND ModifyDate__c < :endDate and PayoffObject__c=:'○' and Status__c IN('対応中' ,'対応完了')];
 //LIST<AggregateResult> calmonth=[Select CALENDAR_MONTH(WorkDate__c), count(ImportCount__c) icount from WorkResultsMonth__c where Account_Name__c=:acname GROUP BY CALENDAR_MONTH(WorkDate__c)];
LIST<AggregateResult> calmonth=[Select count(ImportCount__c) icount from WorkResultsMonth__c where WorkDate__c >= :startDate and WorkDate__c < :endDate and Account_Name__c=:FBR.KPIInquiryContent__r.Account__c];

List<KPIAccident__c> acc= [SELECT MonthlyGeneratedCount__c,MonthlyPPM__c from KPIAccident__c where Account__c=:FBR.KPIInquiryContent__r.Account__c];   
if(!acc.isEmpty() )
      {
          
          KPIAccident__c accupdate = acc[0];         
          Integer totalcountmonth=Integer.Valueof(countmonth[0].get('coun')); 
          
          accupdate.MonthlyGeneratedCount__c=totalcountmonth;   
         
          Integer totalmonthwork= Integer.Valueof(calmonth[0].get('icount'));
          accupdate.MonthlyPPM__c=(totalmonthwork/totalcountmonth*1000000);
          update accupdate;
      }else 
      {
         KPIAccident__c kpi = new KPIAccident__c();
         List<KPIAccident__c> ToUpdate = new List<KPIAccident__c>(); 
        // kpi.Date__c=FBR.ModifyDate__c;         
          kpi.Account__c=FBR.KPIInquiryContent__r.Account__c;         
        // kpi.itemname__c= FBR.CommodityName__c;     
         
         Integer totalcountmonths=Integer.Valueof(countmonth[0].get('coun'));
         kpi.MonthlyGeneratedCount__c=totalcountmonths; 
         Integer totalmonthworks= Integer.Valueof(calmonth[0].get('icount'));
                    
         kpi.MonthlyPPM__c=(totalmonthworks/totalcountmonths*1000000);
         ToUpdate.add(kpi);
         insert ToUpdate;



}
} }
ChermaduraiChermadurai

Hello,

 

Use the below code it reached 61%..Please can any one help to improve the code coverage to above 75%..

 

@isTest
class FBR_ApexScheduleMonthClassTest {
    static testMethod void myUnitTest() {
   
     account a = new account();
        a.name='saaa';
        insert a;
         KPI_StoreInquiryStatus__c a3 = new KPI_StoreInquiryStatus__c(Account__c=a.id,name='Candidate Pool');
        insert a3;
         KPIInquiryStatus__c a4 = new KPIInquiryStatus__c();
         a4.Account__c=a.id;
         insert a4;
           
    test.startTest();
    FBR_Inquiry__c fi=new FBR_Inquiry__c();
   fi.KPIInquiryContent__c=a3.id;
   fi.KpiInquiryRef__c=a4.id;
    fi.CommodityName__c='fgggggj';
    fi.ModifyDate__c=date.parse('2012/10/10');
    fi.Status__c ='hkjcyh';
    insert fi;
     KPIAccident__c kpi = new KPIAccident__c();
       kpi.Account__c=a.id;
    insert kpi;
    FBR_ApexScheduleMonthClass Updates = new FBR_ApexScheduleMonthClass();
    String schedule = '0 0 23 * * ?';
    system.schedule('Nightly Update', schedule, Updates );
        
   
    test.stopTest();
    }
}