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
Dosbol TDosbol T 

Help with test coverage for trigger

Please help with this test class, I cant get 100%, dont know how to improve it. Thanks,
@isTest
private class AutoPopDateTest {
  @isTest
  private static void AutoPopDate() {
    Billing__C bil = new Billing__C();
    bil.Name = 'NewBill';
    bil.Billing_Date__c = system.today();
    bil.Accounting_date__c = bil.Billing_Date__c.addDays(20);
    try {
      insert bil;
    } catch (NullPointerException n) {
    }
  }
}
and trigger:
trigger AutoPopAccPerDate on Billing__c(before insert, before update) {
   for (Billing__c billing : Trigger.new) {
    if (billing.Accounting_Date__c == null) {
      billing.Accounting_Date__c = billing.Billing_Date__c.adddays(30); // '30 Days' - Depends on the terms.
    }
  }
}

Appreciate any support!
 
Best Answer chosen by Dosbol T
Arvind_SinghArvind_Singh
Hello Dosbol

You are checking for if (billing.Accounting_Date__c == null) then populate Accounting_Date__c but in test class you passing value for Accounting_Date__c  so it is not entering in IF condition. 

You can add one more record or comment out line bil.Accounting_date__c = bil.Billing_Date__c.addDays(20) then it should cover 
 
@isTest
private class AutoPopDateTest {
  @isTest
  private static void AutoPopDate() {
    Billing__C bil = new Billing__C();
    bil.Name = 'NewBill';
    bil.Billing_Date__c = system.today();
    //bil.Accounting_date__c = bil.Billing_Date__c.addDays(20); //comment this line
    try {
      insert bil;
    } catch (NullPointerException n) {
    }
  }
}


 

All Answers

Arvind_SinghArvind_Singh
Hello Dosbol

You are checking for if (billing.Accounting_Date__c == null) then populate Accounting_Date__c but in test class you passing value for Accounting_Date__c  so it is not entering in IF condition. 

You can add one more record or comment out line bil.Accounting_date__c = bil.Billing_Date__c.addDays(20) then it should cover 
 
@isTest
private class AutoPopDateTest {
  @isTest
  private static void AutoPopDate() {
    Billing__C bil = new Billing__C();
    bil.Name = 'NewBill';
    bil.Billing_Date__c = system.today();
    //bil.Accounting_date__c = bil.Billing_Date__c.addDays(20); //comment this line
    try {
      insert bil;
    } catch (NullPointerException n) {
    }
  }
}


 
This was selected as the best answer
Dosbol TDosbol T
@Arvind_Singh, Much appreciated for your valuable feedback, thanks a lot!