0

I have an apex class that updates a field. This class is called by two apex triggers from two different custom objects that fire after insert, update, and delete. But after I ran the test class, only the test methods for after insert and update are covered while the method for delete event is not covered at all.

Please also take note that apex class is 100% covered, but only 66% are covered in apex triggers.

Meanwhile, here are the custom objects and fields I am using:

ObjA__c : Id, Name, checkbox__c, Total_ObjB__c, Total_ObjC__c, Grand_Total__c
ObjB__c : Id, Name, ObjA__c, Quantity__c
ObjC__c : Id, Name, ObjA__c, Quantity2__c

I've made these three fields number type: Total_ObjB__c, Total_ObjC__c, Grand_Total__c instead of the standard roll-up summary fields.

Apex Class:

public class SampleController {

    public static void TotalObjBandC(Set<Id> ids) {

        Decimal sum1 = 0;
        Decimal sum2 = 0;
        Decimal Sum3 = 0;
        List<ObjA__c> objAQry = new List<ObjA__c__c>();
        List<ObjA__c> objAList = new List<ObjA__c>();

        objAQry = [SELECT Id, Name, checkbox__c, Total_ObjB__c, Total_ObjC__c, Grand_Total__c, (SELECT Id, Name, Quantity__c FROM ObjBs__r), (SELECT Id, Name, Quantity2__c FROM ObjCs__r) FROM ObjA__c WHERE ID IN: ids];

        for(ObjA__c objA : objAQry) {


            for(objB__c objB : objA.ObjBs__r) {
                if(!objA.checkbox__c) {
                    sum1 += objB.Quantity__c;
                }
                else {
                    objA = objA.Grand_Total__c + objB.Quantity__c;
                }
            }


            for(ObjC__c objC : objA.ObjCs__r) {
                if(!objA.checkbox__c) {
                    sum2 += objC.Quantity2__c;
                }
                else {
                    sum2 = objA.Grand_Total__c + objC.Quantity2__c;
                } 
            }

            objA.Total_ObjB__c = sum1;
            objA.Total_ObjC__c = sum2;
            sum3 = sum1 + sum2;
            objA.Grand_Total__c = sum3;
            objAList.add(objA); 

        }

        update objAList;
    }
}

Apex Trigger on ObjB__c:

trigger updateGrandTotal on ObjB__c (after insert, after update, after delete) {

    Set<Id> Ids = new Set<Id>();

    if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) {
        for(ObjB__c objB : Trigger.new) {
            Ids.add(ObjB.ObjA__c);
        }
    }

    else if(Trigger.isAfter && Trigger.isDelete) {
        for(ObjB__c objB : Trigger.old) {
            Ids.add(objB.objA__c);
        }
    }

    if(!Ids.isEmpty()) {
        SampleController.TotalObjBandC(Ids);
    }

}

Apex Trigger on ObjC__c:

trigger updateGrandTotal2 on ObjC__c (after insert, after update, after delete) {

    Set<Id> Ids = new Set<Id>();

    if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate)) {
        for(ObjC__c objC : Trigger.new) {
            Ids.add(ObjC.ObjA__c);
        }
    }

    else if(Trigger.isAfter && Trigger.isDelete) {
        for(ObjC__c objC : Trigger.old) {
            Ids.add(objC.objA__c);
        }
    }

    if(!Ids.isEmpty()) {
        SampleController.TotalObjBandC(Ids);
    }

}

Apex Test Class:

@isTest  

public class SampleControllerTestClass {

    static List<ObjA__c> objAList = new List<ObjA__c>(); 
    static List<ObjB__c> objBList = new List<ObjB__c>();
    static List<ObjC__c> objCList = new List<ObjC__c>();
    static List<ObjA__c> objAList2 = new List<ObjA__c>();

    private static testMethod void chkboxFalse() {

        ObjA__c objA = new ObjA__c();
        objA.Name = 'Sample ObjA';
        objA.checkbox__c = false;
        objAList.add(objA);
        insert objAList;

        ObjB__c objB = new ObjB__c();
        objB.ObjA__c = objA.Id;
        objB.Quantity__c = 100;  
        objBList.add(objB);
        insert objBList;

        ObjC__c objC = new ObjC__c();
        objC.ObjA__c = objA.Id;
        objC.Quantity2__c = 100;
        objCList.add(objC);
        insert objCList;

        Test.startTest();

        for(ObjA__c objA : objAList) {
            objA.Grand_Total__c = 200;
            objAList2.add(objA);    
        }

        update objAList2;

        SampleController sc = new SampleController();

        Test.stopTest();


    }

    private static testMethod void chkboxTrue() {

        ObjA__c objA = new ObjA__c();
        objA.Name = 'Sample ObjA';
        objA.checkbox__c = true;
        objAList.add(objA);
        insert objAList;

        ObjB__c objB = new ObjB__c();
        objB.ObjA__c = objA.Id;
        objB.Quantity__c = 100;  
        objBList.add(objB);
        insert objBList;

        ObjC__c objC = new ObjC__c();
        objC.ObjA__c = objA.Id;
        objC.Quantity2__c = 100;
        objCList.add(objC);
        insert objCList;

        Test.startTest();

        for(ObjA__c objA : objAList) {
            objA.Grand_Total__c = 200;
            objAList2.add(objA);    
        }

        update objAList2;

        SampleController sc = new SampleController();

        Test.stopTest();


    }

    private static testMethod void deleteMethod() {

        ObjA__c objA = new ObjA__c();
        objA.Name = 'Sample ObjA';
        objA.checkbox__c = false;
        objAList.add(objA);
        insert objAList;

        ObjB__c objB = new ObjB__c();
        objB.ObjA__c = objA.Id;
        objB.Quantity__c = 100;  
        objBList.add(objB);
        insert objBList;

        ObjC__c objC = new ObjC__c();
        objC.ObjA__c = objA.Id;
        objC.Quantity2__c = 100;
        objCList.add(objC);
        insert objCList;

        for(ObjA__c objA : objAList) {
            objA.Grand_Total__c = 200;
            objAList2.add(objA);    
        }

        update objAList2;

        Test.startTest();

        delete objAList2;

        SampleController sc = new SampleController();

        Test.stopTest();


    }

}
1
  • You have not written any test methods here, they are execution scripts and do not actually test anything at all. With the posted code you have absolutely no way of knowing if what should have happened actually did. Try to get out of the habit of writing these types of scripts and calling them test methods ASAP or it will come back to bite you later
    – Eric
    Dec 11 '16 at 20:50
0

According to your trigger, you have mentioned after delete on ObjB__c whereas in the testMethod deleteMethod() you are performing delete operation on List of ObjA__c object. That's why code for delete in trigger is not covered.

So, try to delete objBList in the deleteMethod().

Second thing, try to put System.assert() in test methods.

1
  • Hey, it's working now. Thanks for pointing that one out, my bad. Thanks again :)
    – sfnewbie21
    Dec 11 '16 at 20:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.