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
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) 

//trigger
{ 
   
    // try
    try{
           
        Id recordTypeId =
        Schema.SObjectType.Opportunity.getRecordTypeInfosByDeveloperName().get('CDARS_ICS_Prospect').getRecordTypeId();
        
        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;
                    processedOpportunities.add(OpportunityList.Id);
                        
                    //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;
                        
                        createOpenQuarter.add(oq);
                        quarter++;
                        
                        // if loop 3
                        if (quarter > 4) {
                            quarter = 1;
                            year++;
                        } //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
        if(createOpenQuarter.isEmpty()==false){
            Database.insert (createOpenQuarter,false);
        } // end if loop 5
    } // end try
    
    //catch
    catch (Exception e){
        //e.getMessage()
            //e.getLineNumber()
            throw e;
    } // end catch
} // end trigger
And here is the test class:
@isTest
public class TestCreateOqNew {
    
    @isTest static void testForCreateOQ() {
        
        Account acct = new Account(Name='Steve Test Account',
                                   recordTypeId='01241000001I8KBAA0',
                                   Service_Membership__c='Non-Member',
                                   Type='Bank');
        insert acct;
        
        Opportunity opp = new Opportunity(name=acct.Name + 'Opportunity',
                                          recordTypeId='01241000001USq9AAG',
                                          StageName='Prospecting',
                                          CloseDate=System.today().addMonths(6),
                                          Estimated_Finish_Quarter__c=System.today().addMonths(6),
                                          Estimated_Start_Quarter__c=System.today(),
                                          AccountId=acct.Id,
                                          Objective__c='Onboard - New',
                                          Amount=987654321,
                                          LeadSource='Webinar',
                                          Product_Type__c='CDARS Reciprocal');
        
        
        Test.startTest();
        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];
        Test.stopTest();
        
        
        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,
Steve​​​​​​​​​​​​​​
 
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.

Steve

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.

https://trailhead.salesforce.com/content/learn/modules/apex_testing

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

Thanks.
 
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
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.

Steve
This was selected as the best answer