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
JN22JN22 

Test Class Coverage at 25%

Hello,

I have a trigger on a custom object (Client_Revenue__c).  When a record is created, edited, or deleted for this object, the trigger links the record of the current year (based on a field called Revenue_Year__c in the custom object) to a custom lookup field on the Account.  The test I created is only covering 25% of the trigger.  Lines 12 - 25 and 32-42 are not being covered.  Does anyone know why this would be happening?  Thanks,

Trigger:
trigger Contract2Account on Contract_Summary__c (after insert, after update, after delete) 
{          
    Try
    {
     Map<Id,Account> oppMap = new Map<Id,Account>();
     Set<id> Ids = new Set<id>();

if(trigger.isInsert || trigger.isUpdate)
{
    if(checkRecursiveAI.runOnceAI() || checkRecursiveAU.runOnceAU())
    {
        for (Contract_Summary__c prgm : Trigger.new){
            if(prgm.Type_of_Contract__c == 'Renewal' || prgm.Type_of_Contract__c == 'Initial MSA')
            Ids.add(prgm.Account_Name__c);
        }
     
        Map<id,Account> acctMap2 = new Map<id,Account>([Select Id,Name,Contract_Summary__c, Contract_Summary__r.Current_Effective_Date__c from Account Where Id in :Ids]);  
        for (Contract_Summary__c  prgm2 : Trigger.new){
            if(acctMap2.containsKey(prgm2.Account_Name__c) && (acctMap2.get(prgm2.Account_Name__c).Contract_summary__r.Current_Effective_Date__c < prgm2.Current_Effective_Date__c || acctMap2.get(prgm2.Account_Name__c).Contract_summary__r.Current_Effective_Date__c == null)) {
                Account a = acctMap2.get(prgm2.Account_Name__c);
                a.Contract_Summary__c = prgm2.Id;
                oppMap.put(a.id,a);
            }
        }
        update oppMap.values();
    }
}
     
if(trigger.isDelete)
{
    if(checkRecursiveAD.runOnceAD()){
        for (Contract_Summary__c prgm : Trigger.old){
            if(prgm.Type_of_Contract__c == 'Renewal' || prgm.Type_of_Contract__c == 'Initial MSA')
            Ids.add(prgm.Account_Name__c);
        }
        Map<id,Account> acctMap2 = new Map<id,Account>([Select Id,Name,Contract_Summary__c from Account Where Id in :Ids]);  
        for (Contract_Summary__c  prgm2 : Trigger.old){
                Account a = acctMap2.get(prgm2.Account_Name__c);
                a.Contract_Summary__c = null;
                oppMap.put(a.id,a);
        }
        update oppMap.values();
    }
}
    }
    catch(Exception e)
    {}
}

Test Class:

@isTest(SeeallData=true)
private class ContractSummary_Tests2 {

    private static testmethod void testSummary1() {

    Account a1 = new Account();
        a1.name = 'test';
        a1.Type = 'Employer';
    insert a1;
   
    Opportunity opp1 = new Opportunity();
            opp1.Name = 'Test Opportunity';
            opp1.StageName = 'Stage 6 - Live';
            opp1.CloseDate = date.newinstance(2020,1,31);
            opp1.Type = 'New Business';
            opp1.accountId=a1.Id;
        insert opp1;

    Opportunity opp2 = new Opportunity();
            opp2.Name = 'Test Opportunity';
            opp2.StageName = 'Stage 6 - Live';
            opp2.CloseDate = date.newinstance(2022,1,31);
            opp2.Type = 'Renewal';
            opp2.accountId=a1.Id;
        insert opp2;

    Contract_Summary__c testContSumm1 = new Contract_Summary__c ();
        testContSumm1.Related_Opportunity__c = opp1.Id;
        testContSumm1.Account_Name__c = opp1.Account.id;
        testContSumm1.Current_Effective_Date__c = date.newinstance(2020,1,31);
        testContSumm1.Current_Expiration_Date__c = date.newinstance(2022,1,31);
        testContSumm1.Type_of_Contract__c = 'Initial MSA';
        testContSumm1.Client_Signature_Date__c = date.newinstance(2020,1,31);
    insert testContSumm1;

    Contract_Summary__c testContSumm2= new Contract_Summary__c ();
        testContSumm2.Related_Opportunity__c = opp2.Id;
        testContSumm2.Account_Name__c = opp2.Account.id;
        testContSumm2.Current_Effective_Date__c = date.newinstance(2022,1,31);
        testContSumm2.Current_Expiration_Date__c = date.newinstance(2024,1,31);
        testContSumm2.Type_of_Contract__c = 'Renewal';
        testContSumm2.Client_Signature_Date__c = date.newinstance(2022,1,31);
    insert testContSumm2;

        
    Test.startTest();
    update opp1;
    update opp2;
    update testContSumm1;
    update testContSumm2;
    delete testContSumm2;
    Test.stopTest();

    }
}

Class to prevent infinite loops:
public Class checkRecursiveAI{
    private static boolean run = true;
    public static boolean runOnceAI(){
    if(run){
     run=false;
     return true;
    }
    else{
        return run;
    }
    }
}



Best Answer chosen by JN22
Prafull G.Prafull G.
Can you try to see debug logs for this test class execution? The code and execution seems to be pretty simple here.
Enable debug and try to capture if we can get some details about the code skip.

All Answers

Prafull G.Prafull G.
Try followings:
- move insert statements after Trigger.startTest().
- remove try catch block from trigger to check if you are getting some exception
JN22JN22
Hi Prafull,

I tried that but it does not increase coverage and the test does still pass.  Any other ideas?
Prafull G.Prafull G.
Can you try to see debug logs for this test class execution? The code and execution seems to be pretty simple here.
Enable debug and try to capture if we can get some details about the code skip.
This was selected as the best answer
JN22JN22
Thanks.  I checked and it look like there was actually another after Insert/Update/Delete trigger on the same object.  Because I had the CheckRecursive code in the trigger, it was only executing one.  I combined the 2 and now have 100% coverage.  Thanks for all the help!