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 

Test class code coverage problem-please help me

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 = '' ) ];

below  red lines  are not coverd…..
    if (va1count > 0)
       
       { 
         system.debug('value of count:'+va1count);      
         DateTime dT = System.now();
         myDate = date.newinstance(dT.year(), dT.month(), dT.day());
         sdate = String.valueOf(myDate);
         do 
            {
                 system.debug('coming to calculate random number');
                 double d=math.random()*2;
                 randomgen=sdate+ '-' + string.valueof(d.intvalue());
                 system.debug('the random number value is:'+randomgen);
                 rcnt = [ select count() from vatimesheet__c where backupid__c = :randomgen];
             }
         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 or Backupid__c = '')];
         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; 
         system.debug('the row is inserted');
         update va3;
         system.debug('the row is updated');
         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);
             }
                       
                   
       }
     
  }    
}

--------------------------------------------------------------------------------------------------------------------

testclass:

@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));     
    
       } 
       
 }
GlynAGlynA

I think your problem is that the test method schedules your class and then ends - the execute() method of the class is not executed within the context of the test.  To cover everything, don't schedule the class.  Just call the execute() method:

 

(new populatevatimesheetrepository()).execute( (SchedulableContext) null );

 

This will run the execute() method within the context of the test.

 

If this solves your problem, please mark it as the solution.  Thanks!

 

-Glyn

neshnesh

thanks for reply still not cover below lines


         system.debug('value of count:'+va1count);      
         DateTime dT = System.now();
         myDate = date.newinstance(dT.year(), dT.month(), dT.day());
         sdate = String.valueOf(myDate);
         do 
            {
                 system.debug('coming to calculate random number');
                 double d=math.random()*2;
                 randomgen=sdate+ '-' + string.valueof(d.intvalue());
                 system.debug('the random number value is:'+randomgen);
                 rcnt = [ select count() from vatimesheet__c where backupid__c = :randomgen];
             }
         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 or Backupid__c = '')];
         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; 
         system.debug('the row is inserted');
         update va3;
         system.debug('the row is updated');
         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);
             }

GlynAGlynA

nesh,

 

It seems like the code is not finding the VATimesheet__c record that the test code creates.  I'm not sure if this will help, but try my versions and see if the code coverage is acceptable.

 

-Glyn

 

global class populatevatimesheetrepository implements Schedulable
{
    global void execute( SchedulableContext SC )
    {
        List<VATimesheet__c> list_VATimesheets = 
        [   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
                        OR  BackUpID__c = ''
                        )
                    )
        ];

        List<VAtimesheetRepository__c> list_VATimesheetRepositories = new List<VAtimesheetRepository__c>();

        if ( list_VATimesheets.isEmpty() ) return;

        String randomgen;

        //  note that if both backup ids already exists, i.e. today's date-0 and today's date-1,
        //  then this is an infinite loop and will eventually exceed the CPU time limit.
        //  why not use a backup id which is String.valueOf( System.now() )? - this is unique to
        //  the millisecond
        Integer rcnt;
        do
        {
            randomgen = String.valueOf( Date.today() ) + '-' + String.valueOf( (Math.random()*2).intValue() );
            rcnt = [SELECT COUNT() FROM VATimesheet__c WHERE BackUpId__c = :randomgen];
        }
        while( rcnt > 0 );

        for ( VATimesheet__c vt : list_VATimesheets )
        {
            vt.BackUpID__c = randomgen;

            list_VATimesheetRepositories.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 list_VATimesheetRepositories;
        update list_VATimesheets;

        //  not sure why this is necessary...
        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 ];
        if ( vtcnt != vtrcnt ) database.rollback( sp );
    }
}

 

@isTest(seealldata=true)
private class VAtimesheetRepository1
{
    public static testmethod void m0()
    {
        Account a1 = new Account( name = 'AA' );
        insert a1;

        our_va__c v1 = new our_va__c( name = 'v1' );
        insert v1;

        //  this doesn't seem to be used in the test
        insert new Contact( LastName = 'cccccc', AccountID = a1.Id, Primarycontact__c = true );

        insert new VATimesheet__c
        (
            TimeCardDate__c = Date.parse( '8/10/2013' ),
            Status__c       = 'Processed',
            Type__c         = 'Working',
            AccountId__c    = a1.Id,
            VAId__c         = v1.Id,
            OT_Hrs__c       = 0,
            Billed_Hrs__c   = 5.0,
            BackUpID__c     = null
        );

        Test.startTest();
        {
            (new populatevatimesheetrepository()).execute( (SchedulableContext) null );
        }
        Test.stopTest();
    }
}