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
Kris WebsterKris Webster 

Why Isn't My Test Class Testing my REAL Class

I have a standard cladd, a schedulable class, and a test class. When I run my test on the test class the schedulable class passes 100% coverage, however the test calss still sits at 0% coverage, which tells me that the test class may not be testing the standard class at all?? I NEED HELP PLEASE haha. 

Here is the standard class 
 
global class TCApprovalCompliance implements Database.Batchable<sObject>
{
    
   global Database.queryLocator start(Database.BatchableContext ctx )
   {
        String str = 'SELECT Id, pse__Submitted__c, pse__Start_Date__c, pse__Approved__c FROM pse__Timecard_Header__c WHERE pse__Submitted__c = TRUE';
        
        return Database.getQueryLocator(str);
        
   }
    
    global void execute(Database.BatchableContext ctx, List<pse__Timecard_Header__c> nonApprovedTCs)
     {
        
       List<pse__Timecard_Header__c> TCList = new List<pse__Timecard_Header__c>();
       
       for(pse__Timecard_Header__c tcObj : nonApprovedTCs){
            tcObj.Approval_Compliance__c = true;
            TCList.add(tcObj);
          }
        
        update TCList;
     }
   
   global void finish(Database.BatchableContext ctx)
    {
   
      AsyncApexJob a = [SELECT Id, Status, ExtendedStatus, NumberOfErrors, JobItemsProcessed,
                          TotalJobItems, CreatedBy.Email
                          FROM AsyncApexJob WHERE Id =
                          :ctx.getJobId()];
   // Send an email to the Apex job's submitter notifying of job completion. 
    
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {a.CreatedBy.Email};
   mail.setToAddresses(toAddresses);
   mail.setSubject('Apex Sharing Recalculation ' + a.Status);
   mail.setPlainTextBody
   ('The batch Apex job processed ' + a.TotalJobItems +
   ' batches with '+ a.NumberOfErrors + ' failures. The error is related to ' + a.ExtendedStatus);
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
           
    }    
}

Here is the test class...
 
@isTest
public class TCApprovalCompliance_TEST  {

    static testMethod void testTCApprovalCompliance() {
       
        //loadSettings();
        disableTriggerHandlers(true);
        TestDataFactory.initPermissionControls();

        TestDataFactory.timecardHeaders = TestDataFactory.createTimecardHeaders(1, false);
        TestDataFactory.timecardHeaders[0].pse__Start_Date__c = date.parse('09/01/2019');
        TestDataFactory.timecardHeaders[0].pse__End_Date__c = date.parse('09/07/2019');
        TestDataFactory.TimecardHeaders[0].pse__Status__c = 'Submitted';
        TestDataFactory.TimecardHeaders[0].pse__Submitted__c = TRUE;
        TestDataFactory.TimecardHeaders[0].pse__Approved__c = FALSE;
        
        Test.startTest();
        insert TestDataFactory.timecardHeaders;
        system.debug('Cb is checked?? ' + testdatafactory.timecardHeaders[0].Approval_Compliance__c);
        system.debug('Start date is... ' + testdatafactory.timecardHeaders[0].pse__Start_Date__c);
        system.debug('submitted is... ' + testdatafactory.timecardHeaders[0].pse__Submitted__c);
        system.debug('approved is... ' + testdatafactory.timecardHeaders[0].pse__Approved__c);
        system.debug('The Id is... ' + testdatafactory.timecardHeaders[0].Id);
        Test.stopTest();

     TCApprovalCompliance_Schedulable TC1 = new TCApprovalCompliance_Schedulable();
        String sch = '0 0 23 * * ?'; 
        system.schedule('Test Update Contacts Check', sch, TC1); 

        
    }
        
        
    private static void loadSettings()
    {
        TestDataFactory.loadAllConfigGroupsOptionsValues();
        TestDataFactory.updateAsmTriggerSettings(TestDataFactory.getDefaultDisableAsmTriggerSettings());
        TestDataFactory.createSRPIntegrationSettings(true, true);
        TestDataFactory.createProjectTriggerSettings(true);
        TestDataFactory.createTimecardComplianceSettings(true);
        TestDataFactory.createCommonSettingsSettings(true);
    }


    private static void disableTriggerHandlers(boolean disableTimecardTriggers)
    {
        TriggerHandlerBase.disableAllTriggerHandlers(Account.sObjectType);
        TriggerHandlerBase.disableAllTriggerHandlers(Contact.sObjectType);
        TriggerHandlerBase.disableAllTriggerHandlers(pse__Region__c.sObjectType);
        TriggerHandlerBase.disableAllTriggerHandlers(pse__Practice__c.sObjectType);
        TriggerHandlerBase.disableAllTriggerHandlers(pse__Grp__c.sObjectType);
        TriggerHandlerBase.disableAllTriggerHandlers(pse__Proj__c.sObjectType);
        TriggerHandlerBase.disableAllTriggerHandlers(pse__Assignment__c.sObjectType);

        if (disableTimecardTriggers == true)
        {
            TriggerHandlerBase.disableAllTriggerHandlers(pse__Timecard_Header__c.sObjectType);
            TriggerHandlerBase.disableAllTriggerHandlers(pse__Timecard__c.sObjectType);
        }
    }
}

and here is the schedulable class..
 
global class TCApprovalCompliance_Schedulable implements Schedulable {
      global void execute(SchedulableContext ctx) {
           database.executeBatch(new TCApprovalCompliance(),200);
      }
}

 
Sainath VenkatSainath Venkat
@krls,

if you look at the test class line no.4, method name is TCApprovalCompliance() and if you look at your batch class name it is same as TCApprovalCompliance(), so batch class is getting 100% code coverage.

schedulable class is doing nothing but just calling the batch class so the moment you run test then both schedulable and batch class will get code coverage.

Test class dont need code coverage basically, we will write them to get code coverage for actual class, when we run test class they wont come under code coverage so 0% will be shown.

Mark it as best if it helps you.