+ Start a Discussion
Steve Connelly 5Steve Connelly 5 

I could use a little help with a test class for a trigger.

My trigger works and my Test Class have 85% coverage but I would like to know how to get a littl emore coverage.

Not so much for the coverage itself but for the knowlege of how to do this in the future.

My trigger runs for inserts and updates.

My test class covers the insert but not the update and i am not at all; sure how to do both.

Here is the trigger:
trigger createOqNew on Opportunity (after insert, after update) 

    // try
        Id recordTypeId =
        List <Open_Quarter__c> createOpenQuarter = new List <Open_Quarter__c>();
        List <Open_Quarter__c> deleteOpenQuarter = new List <Open_Quarter__c>();
        Set <Id> processedOpportunities = new Set <Id>();
        // for loop 1
        for (Opportunity opportunityList : Trigger.new) {   
            // if loop 1
            if (opportunityList.RecordTypeId == recordTypeId) {
                // if loop 2
                if (Trigger.isInsert 
                    || (Trigger.isUpdate && opportunityList.Estimated_Start_Quarter__c != Trigger.oldMap.get(opportunityList.Id).Estimated_Start_Quarter__c)
                    || (Trigger.isUpdate && opportunityList.Estimated_Finish_Quarter__c != Trigger.oldMap.get(opportunityList.Id).Estimated_Finish_Quarter__c)){
                    Decimal year = opportunityList.Start_Year__c;
                    Decimal quarter = opportunityList.Start_Quarter__c;
                    Decimal span = opportunityList.Quarters_Spanned_op__c;
                    //for loop 2
                    for (Integer counter = 0; counter < span; counter++)
                        Open_Quarter__c oq           		= new Open_Quarter__C();
                        oq.Amount_Per_Quarter_oq__c  		= opportunityList.Amount_per_Quarter_op__c;
                        oq.Estimated_Finish_Quarter_OQ__c   = opportunityList.Estimated_Finish_Quarter__c;
                        oq.Estimated_Start_Quarter_OQ__c	= opportunityList.Estimated_Start_Quarter__c;
                        oq.Name                      		= year+'-'+'Q'+quarter;
                        oq.Opportunity_Name_oq__c    		= opportunityList.Id;
                        // if loop 3
                        if (quarter > 4) {
                            quarter = 1;
                        } //end if loop 3                       
                    } //end for loop 2      
                } //end if loop 2
            } //end if loop 1
        } //end for loop 1
        deleteOpenQuarter.addAll ([SELECT Id, Name FROM Open_Quarter__c WHERE Opportunity_Name_oq__c IN :processedOpportunities]);
        // if loop 4
        if (deleteOpenQuarter.isEmpty()==false){
            Database.delete (deleteOpenQuarter,false);
        } // end if loop 4
        // if loop 5
            Database.insert (createOpenQuarter,false);
        } // end if loop 5
    } // end try
    catch (Exception e){
            throw e;
    } // end catch
} // end trigger
And here is the test class:
public class TestCreateOqNew {
    @isTest static void testForCreateOQ() {
        Account acct = new Account(Name='Steve Test Account',
        insert acct;
        Opportunity opp = new Opportunity(name=acct.Name + 'Opportunity',
                                          Objective__c='Onboard - New',
                                          Product_Type__c='CDARS Reciprocal');
        insert opp;
        List<Open_Quarter__c> quarters = [SELECT Id, Name, Amount_Per_Quarter_oq__c, Estimated_Finish_Quarter_OQ__c, Estimated_Start_Quarter_OQ__c  , Opportunity_Name_oq__c FROM Open_Quarter__c ORDER BY Name];
        System.assertEquals(3, quarters.size());
        System.assertEquals('2020-Q2', quarters[0].name);            

This is the main bit that is not covered:
|| (Trigger.isUpdate && opportunityList.Estimated_Start_Quarter__c != Trigger.oldMap.get(opportunityList.Id).Estimated_Start_Quarter__c)
                    || (Trigger.isUpdate && opportunityList.Estimated_Finish_Quarter__c != Trigger.oldMap.get(opportunityList.Id).Estimated_Finish_Quarter__c)){

(the "OR" parts).

Any tips on how to cover the update as well as the insert?

Thanks much,
Best Answer chosen by Steve Connelly 5
Steve ConnellySteve Connelly
I was able to sort this out to the point that i have 94% coverage. I am good with that for this project.


All Answers

AbhishekAbhishek (Salesforce Developers) 
Hi Steve,

If you really want 100% code coverage you really need to write test methods that test all of these 20 methods.

You can skip asserts if you just want the code coverage although I would not recommend it

You need to write tests that cover each of the different methods. The Apex Testing Trailhead module explains how to do this.


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Steve ConnellySteve Connelly
I took that full trailhead already. It really did not help me with my question at all. As with most trailheads it does a good job of proving a broad view of the subject but rarely addresses specific how-tos.

I see how to run multiple tests I am just not sure how to test the update portion.

Any help out there?

Steve ConnellySteve Connelly
I was able to sort this out to the point that i have 94% coverage. I am good with that for this project.

This was selected as the best answer