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
KatherineCKatherineC 

How to increase test class coverage?

Hi All,

We want a new Pint is self-generated once checkbox Oct31 is checked (true), account and pint owner have to be the same, new pint's payroll date wil be 16 days more and new pint's schedule start date will be 9 days more of old pint's payroll date. I got this trigger and test class, the coverage is 70%, did not pass, please help me write the test class to increase coverage. Many thanks.

trigger Oct31Pint on Pint__c (after update) {
    List<Pint__c> pints = new List<Pint__c>();
   for (Pint__c p : Trigger.new) {
           if (p.Oct31__c == true ) {
               Pint__c newPint = new Pint__c();
               newPint.Account__c = p.Account__c;
               newPint.Pint_Owner__c = p.Pint_Owner__c;
               newPint.Payroll_Date__c = p.Payroll_Date__c + 16;
               newPint.Schedule_Start_Date_5_Biz_Days_Prior_PR__c = p.Payroll_Date__c + 9;
               pints.add(newPint);
        }
    }
   if(pints.size() > 0) insert pints;
}


TEST:

@isTest
public class TestOct31Pint
{
    static testMethod void insertNewPint()
    {
        Account account = new Account();
        account.Name = 'Test Account';
        insert account;
        Pint__c PintToCreate = new Pint__c();
        PintToCreate.Account__c = account.Id;
        insert PintToCreate;
        Test.StartTest();
        PintToCreate.Oct31__c = true;
        update PintToCreate;
        Test.StopTest();
    }
}
Best Answer chosen by KatherineC
Shingo YamazakiShingo Yamazaki
This test fails at line 9 because you don't set the value on the field "Payroll_Date__c".

That's why the coverage is 70%.

All Answers

Shingo YamazakiShingo Yamazaki
This test fails at line 9 because you don't set the value on the field "Payroll_Date__c".

That's why the coverage is 70%.
This was selected as the best answer
Shingo YamazakiShingo Yamazaki
Sorry, fails at line 8, not line 9.
But the reason is same.
KatherineCKatherineC
Thanks for the reply, sorry I don't know how to set the payroll date value, I add a line of code as below - Pint__c.Payroll_Date__c = Today, but it says variable Today does not exist, please advise.

@isTest
public class TestOct31Pint
{
    static testMethod void insertNewPint()
    {
        Account account = new Account();
        account.Name = 'Test Account';
        insert account;
        Pint__c.Payroll_Date__c = Today;
        Pint__c PintToCreate = new Pint__c();
        PintToCreate.Account__c = account.Id;
        insert PintToCreate;
        Test.StartTest();
        PintToCreate.Oct31__c = true;
        update PintToCreate;
        Test.StopTest();
    }
}
KatherineCKatherineC
I figured it out and got 100% coverage now, add payroll value on line 9 as below, thanks for the help Shingo.

Pint__c PintToCreate = new Pint__c(Payroll_Date__c = Date.today());
Shingo YamazakiShingo Yamazaki
Glad to hear that!