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
neshnesh 

need help-Test class code coverage problem

My apex class -only covered 36%.Please help me-get more coverage(above 75%).....

APEXCLASS:

global class populatevatimesheetrepository implements Schedulable
{
  public integer inscount=0;
  public integer updtcount=0;
  public integer rcnt;
  public string randomgen;
  Public Date mydate{get;set;}
  public String sdate {get;set;}
        
  global void execute(SchedulableContext SC)
  {
    
    list<VATimesheet__c> va1 = new list<VATimesheet__c>();
    list<VAtimesheetRepository__c> va2 = new list<VAtimesheetRepository__c>();
    list<VATimesheet__c> va3 = new list<VATimesheet__c>();
    list<VAtimesheetRepository__c> va4 = new list<VAtimesheetRepository__c>();
        
    //** random no logic
    
    integer va1count=[select count() from VATimesheet__c where Status__c='Processed' and ( BackUpID__c = NULL or Backupid__c = '' ) ];
    if (va1count > 0)
       {
         do 
            {
                 double d=math.random()*2;
                 DateTime dT = System.now();
                 myDate = date.newinstance(dT.year(), dT.month(), dT.day());
                 sdate = String.valueOf(myDate);
                 randomgen=sdate+ '-' + string.valueof(d.intvalue());
                 system.debug('the random number value is:'+randomgen);
                 rcnt = [ select count() from vatimesheet__c where backupid__c = :randomgen];
                 if (rcnt==0)
                     {
                       break;
                      }
            }
         while(rcnt == 0);
         
         va1=[select id,AccountId__c,AccountName__c,AcctID__c,Aname__c,BackUpID__c,BCRNo__c,BCRRecID__c,
                     Billed_Hrs__c,Contract_Hours__c,chrs__c,Job_type__c,OT_Hrs__c,VAId__c,Payrollno__c,
                     Payroll_status__c,srchstring__c,Status__c,TimeCardDate__c,Type__c,VA__c,VAName__c,
                     Vname__c from VATimesheet__c where Status__c='Processed' and BackUpID__c=NULL];
         for(VATimesheet__c vt:va1)
            {
              system.debug('The backup id value is :' + randomgen);
              
              //***updating time sheet object
              vt.BackUpID__c=randomgen;
              va3.add(vt); 
              
              //** inserting into timesheet repository
               va2.add(new VAtimesheetRepository__c(AccountId__c=vt.AccountId__c,Aname__c=vt.Aname__c,BackUpID__c=vt.BackUpID__c,BCRNo__c=vt.BCRNo__c,
                      BCRRecID__c=vt.BCRRecID__c,Billed_Hrs__c=vt.Billed_Hrs__c,Contract_Hours__c=vt.Contract_Hours__c,Job_type__c=vt.Job_type__c,
                      OT_Hrs__c=vt.OT_Hrs__c,VAId__c=vt.VAId__c,Payrollno__c=vt.Payrollno__c,Payroll_status__c=vt.Payroll_status__c,
                      Status__c=vt.Status__c,TimeCardDate__c=vt.TimeCardDate__c,Type__c=vt.Type__c));
            }
         insert va2; 
         update va3;
         savepoint sp=database.setsavepoint(); 
         
         integer vtcnt = [ select count() from vatimesheet__C where backupid__c = :randomgen ];
         integer vtrcnt = [ select count() from vatimesheetrepository__C where backupid__c = :randomgen ];
                 
         try
           {
             system.assertequals(vtcnt,vtrcnt);
              
             } 
          catch(exception e)
            {
              database.rollback(sp);
             }
                       
                   
       }
     
  }    
}

 TEST CLASS:

@isTest(seealldata=true)
private class VAtimesheetRepository1
{
  //*****************************************************************************
  //** this Test class will test the VAtimesheet and VAtimesheetRepository      *
  //*****************************************************************************
   public static String CRON_EXP = '0 0 0 22 5 ? 2020';
    
    
  Public static testmethod void m0()
    {
        account a1 = new account();
     a1.name = 'AA';
     insert a1;
         
     our_va__c v1 = new our_va__c();
     v1.name = 'v1';
     insert v1;
     
     contact c1= new contact();
     c1.LastName='cccccc';
     c1.Accountid=a1.id;
     c1.Primarycontact__c=true;
     insert c1;
         
     vatimesheet__c  v2 = new vatimesheet__c();
     v2.TimeCardDate__c= date.parse('8/10/2013'); 
     v2.Status__c='Processed';
     v2.Type__c='Working';
     v2.AccountId__c=a1.id;
     v2.VAId__c=v1.id;
     v2.OT_Hrs__c= 0; 
     v2.Billed_Hrs__c=5.0;
     v2.BackUpID__c=NULL;
     insert v2;
    
      
    VAtimesheetRepository__c v3= new VAtimesheetRepository__c();  
    insert v3;  
              
      String jobId = System.schedule('ScheduleApexClassTest', CRON_EXP, new populatevatimesheetrepository());
       CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId];
         //Verify the expressions are the same
        System.assertEquals(CRON_EXP,ct.CronExpression);
         //Verify the job has not run
        System.assertEquals(0,ct.TimesTriggered);
          //Verify the next time the job will run
        System.assertEquals('2020-05-22 00:00:00',
       String.valueOf(ct.NextFireTime));     
    
       } 
       
 }
Vinita_SFDCVinita_SFDC

Hello,

 

In test class, you are inserting V3 and testing it in assert method. Now update V3 in test class and test it in assert method. This will increase coverage.