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
Josh HarshJosh Harsh 

Apex Class For Full Coverage of Trigger

Hello,

Below are a trigger and its test class. Understanding that the coverage needs to be 75% in order to send it to production, I seem to be stuck with my code, and I am hoping that someone may be able to push me in the right direction.


trigger updatezipcode on Account (before update, before insert) {
    List<String> BillingPostalCodes = new List<String>();
   
    for (Account a:Trigger.new){
        BillingPostalCodes.add(a.BillingPostalCode.substring(0,5));
    }

    List <Zip_Code__c> ZipCodeList = [Select ID, Name from Zip_Code__c where Name in :BillingPostalCodes];

    for (Integer i = 0; i <Trigger.new.size(); i++){
        if (ZipCodeList.size() > 0 && Trigger.new[i].BillingPostalCode.substring(0,5) !=null){
                        for (Zip_Code__c z:ZipCodeList){
                if (Trigger.new[i].BillingPostalCode.substring(0,5) == z.name){
                        Trigger.new[i].Zip_Code_Territory__c = z.ID;

                                }
                        }
        }
        else{
        Trigger.new[i].Zip_Code_Territory__c = null;
        }
       
    }
}

@isTest
private class TestZip {
    static testMethod void testZipChange() {
        Account[] Accounttest = new Account[]{
         new Account(Name ='Tester1', BillingPostalCode = '44281'),
         new Account(Name ='Tester2', BillingPostalCode = '44282'),
         new Account(Name ='Tester3', BillingPostalCode = '44283'),
         new Account(Name ='Tester4', BillingPostalCode = '44284'),
         new Account(Name ='Tester5', BillingPostalCode = '44285'),
         new Account(Name ='Tester6', BillingPostalCode = '44286',OwnerId = '005C0000006nFc6')
         };
         insert Accounttest;
           
        Test.startTest();
        Accounttest[0].BillingPostalCode = '44281';
        update Accounttest;
        Test.stopTest();
       
        Accounttest = [SELECT id, BillingPostalCode, Zip_Code_Territory__c FROM Account WHERE id IN:Accounttest];


    }
}

The bolded area in the trigger is what is not covered in the class. 

Thank you for any help!
Vi$hVi$h
In the Test class, records are inserted in accounts but nothing is inserted in Zip_Code__c.
Due to this ZipCodeList may be null & the for loop for it might not be covered.



Josh HarshJosh Harsh
DO you have any advise on how I could cover that portion.

I did create a class that creates Zip Codes too, off the Zip_Code__c object. See below.

@isTest
private class TestAddZip {
    static testMethod void testAddZipChange() {
        Zip_Code__c[] Ziptest = new Zip_Code__c[]{
         new Zip_Code__c(Name = '44281'),
         new Zip_Code__c(Name = '44282'),
         new Zip_Code__c(Name = '44283'),
         new Zip_Code__c(Name = '44284'),
         new Zip_Code__c(Name = '44285'),
         new Zip_Code__c(Name = '44286',OwnerId = '005C0000006nFc6')
         };
         insert Ziptest;
           
        Test.startTest();
        Ziptest[0].Name = '44281';
        update Ziptest;
        Test.stopTest();



    }
}

Its basically the same as the previous class only related to the Zip_Code__c object.
Vi$hVi$h
The code in the method testAddZipChange(), move this code to testZipChange() of the previous test class before insert of account.
For every test method data needs to be created, creating it in one will not make it available in other test method.

Thanks.
Josh HarshJosh Harsh
Thanks, I still did not achieve 75% coverage. Do you think if I add lines to the trigger code that would be covered would help? I have physically tested the trigger and it works flawlessly. What would be som lines of apex code that could be added to not change the effect of the code but give it more of a body?
Vi$hVi$h
Which are the lines that are not covered ?
Adding lines would help but that is not a good thing to do !