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
salesforce_hoonigansalesforce_hoonigan 

Test Class help: Business Hours

Hi All,

I am a newb on Apex and I am not sure if I built my test class correctly. I am only getting 63% code coverage. Any help is gladly appreciated.

Trigger:  (I am getting red marks on 14,15,18 and 21)
trigger TotalBusinessHrs on Predecessor_Task__c (before insert, before update) {

    BusinessHours stdBusinessHours = [select id from BusinessHours where Name = 'Default'];
    Decimal Diff1;

for (Predecessor_Task__c pt1: trigger.new)
{
   
    DateTime d1 = DateTime.newInstance(1997, 1, 31, 7, 8, 16);
    DateTime d2 = DateTime.newInstance(1997, 1, 31, 7, 8, 16);
    
    if((pt1.Actual_Start_Date_Time__c!=null) && (pt1.Actual_Completion_Date_Time__c!=null)) {
        
    d1=pt1.Actual_Start_Date_Time__c;
    d2=pt1.Actual_Completion_Date_Time__c;
    
    
    Diff1=BusinessHours.diff(stdBusinessHours.id,d1,d2);
    
    
    pt1.Actual_of_Hours_of_Completion__c=(Diff1/(1*60*60*1000));
    
    }
  }
}
Test Class:
 
@isTest
private class testTotalBusinessHrs{

    public static testMethod void BusinessHrs() {
    
    Decimal Diff1;
    
    // Setup test data
    Project_and_Task__c Proj = new Project_and_Task__c (
    Name = 'Test1');
    insert Proj;
    
    Predecessor_Task__c pt = new Predecessor_Task__c (
    Project_and_Task__c=Proj.Id,
    Activity_Name__c='Test123', 
    Actual_Start_Date_Time__c=System.now(), 
    Alloted_Hours__c=2.00);
    insert pt;
    
    System.debug('Inserting PT with valid Actual_of_Hours_of_Completion__c Num value');
    
    BusinessHours stdBusinessHours = [select id from BusinessHours where Name = 'Default'];
    
    Proj = [select Id, Name from Project_and_Task__c where Name= 'Test1'];
   
    pt = [select Id, Activity_Name__c, Actual_Start_Date_Time__c, Alloted_Hours__c, Actual_of_Hours_of_Completion__c from Predecessor_Task__c 
            where Activity_Name__c= 'Test123'];
                        
    System.assert (pt.Alloted_Hours__c != NULL); 
    System.assert (pt.Actual_Start_Date_Time__c!= NULL); 
    System.assertequals(pt.Actual_of_Hours_of_Completion__c, pt.Actual_of_Hours_of_Completion__c);
    }
}


 
Best Answer chosen by salesforce_hoonigan
Alexander TsitsuraAlexander Tsitsura
Hi

Maybe you need populate Actual_Completion_Date_Time__c field.

Try this code
@isTest
private class testTotalBusinessHrs{

    public static testMethod void BusinessHrs() {
    
    Decimal Diff1;
    
    // Setup test data
    Project_and_Task__c Proj = new Project_and_Task__c (
    Name = 'Test1');
    insert Proj;
    
    Predecessor_Task__c pt = new Predecessor_Task__c (
    Project_and_Task__c=Proj.Id,
    Activity_Name__c='Test123', 
    Actual_Start_Date_Time__c=System.now(), 
    Actual_Completion_Date_Time__c = System.now(),
    Alloted_Hours__c=2.00);
    insert pt;
    
    System.debug('Inserting PT with valid Actual_of_Hours_of_Completion__c Num value');
    
    BusinessHours stdBusinessHours = [select id from BusinessHours where Name = 'Default'];
    
    Proj = [select Id, Name from Project_and_Task__c where Name= 'Test1'];
   
    pt = [select Id, Activity_Name__c, Actual_Start_Date_Time__c, Alloted_Hours__c, Actual_of_Hours_of_Completion__c from Predecessor_Task__c 
            where Activity_Name__c= 'Test123'];
                        
    System.assert (pt.Alloted_Hours__c != NULL); 
    System.assert (pt.Actual_Start_Date_Time__c!= NULL); 
    System.assertequals(pt.Actual_of_Hours_of_Completion__c, pt.Actual_of_Hours_of_Completion__c);
    }
}

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi

Maybe you need populate Actual_Completion_Date_Time__c field.

Try this code
@isTest
private class testTotalBusinessHrs{

    public static testMethod void BusinessHrs() {
    
    Decimal Diff1;
    
    // Setup test data
    Project_and_Task__c Proj = new Project_and_Task__c (
    Name = 'Test1');
    insert Proj;
    
    Predecessor_Task__c pt = new Predecessor_Task__c (
    Project_and_Task__c=Proj.Id,
    Activity_Name__c='Test123', 
    Actual_Start_Date_Time__c=System.now(), 
    Actual_Completion_Date_Time__c = System.now(),
    Alloted_Hours__c=2.00);
    insert pt;
    
    System.debug('Inserting PT with valid Actual_of_Hours_of_Completion__c Num value');
    
    BusinessHours stdBusinessHours = [select id from BusinessHours where Name = 'Default'];
    
    Proj = [select Id, Name from Project_and_Task__c where Name= 'Test1'];
   
    pt = [select Id, Activity_Name__c, Actual_Start_Date_Time__c, Alloted_Hours__c, Actual_of_Hours_of_Completion__c from Predecessor_Task__c 
            where Activity_Name__c= 'Test123'];
                        
    System.assert (pt.Alloted_Hours__c != NULL); 
    System.assert (pt.Actual_Start_Date_Time__c!= NULL); 
    System.assertequals(pt.Actual_of_Hours_of_Completion__c, pt.Actual_of_Hours_of_Completion__c);
    }
}

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
This was selected as the best answer
Alexander TsitsuraAlexander Tsitsura
Also u can view uncoverage lines https://help.salesforce.com/apex/HTViewHelpDoc?id=code_dev_console_tests_coverage.htm&language=en (https://help.salesforce.com/apex/HTViewHelpDoc?id=code_dev_console_tests_coverage.htm&language=en)
salesforce_hoonigansalesforce_hoonigan
Thanks Alex. It works. Sorry. I missed that.