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
Michael MMichael M 

Test class assistance xyz

Hello, My test class is only covering 70% of my class. How can I get it to cover 100%?

Here is the Class:
public class CBOHandler {
    public static void onAfterUpdate(List<CBO__c> CBOList, Map<Id, CBO__c> oldCBOMap) {

        for (CBO__c cbo : CBOList){
            if(oldCBOMap.get(cbo.Id).Pending_for_5_min__c == false && cbo.pending_for_5_min__c == true){
                         Set<string> cboSet = new Set<string>();
                            cboSet.add(cbo.id);
                            List<Discharge__c> disList = [select id,Referrals_Status__c from discharge__c where id in (select discharge_name__c from cbo__c where id in :cboset )];
                for (Discharge__c dis : disList){
                    if (dis.Referrals_Status__c != 'Pending Outcome'){
                        dis.Referrals_Status__c = 'Pending Outcome';
                    }
                     update disList;
                }
               
            }
            
        }
            
        
}
}


This portion is not being covered by the test:
                    if (dis.Referrals_Status__c != 'Pending Outcome'){
                        dis.Referrals_Status__c = 'Pending Outcome';
                    }
                     update disList;
                }



Here is the Test:
@isTest
public class CBOHandlerTest {
@isTest
    Static Void CboHandlerTestMethod(){
            Discharge__c d = new Discharge__c();
            d.Name = 'Test';
            d.Referrals_Status__c='Documents Uploaded';
                insert d;
        
        Cbo__c c = new Cbo__c();
        c.Pending_for_5_min__c = false;
        insert c;
        
         c = [SELECT Id, name FROM CBO__c WHERE Id =:c.Id];
        c.Pending_for_5_min__c = true;
        update c;
        system.debug(d.Referrals_Status__c);
 

  }
}
Best Answer chosen by Michael M
Poornima HR 5Poornima HR 5
In your test class, there is no relationship established between the object records Cbo__c and Discharge__c. If discharge_name__c is a lookup field in cbo__c, then you may have to write this line Cbo__c.discharge_name__c = d.Id before inserting Cbo__c record and check. 

All Answers

Poornima HR 5Poornima HR 5
In your test class, there is no relationship established between the object records Cbo__c and Discharge__c. If discharge_name__c is a lookup field in cbo__c, then you may have to write this line Cbo__c.discharge_name__c = d.Id before inserting Cbo__c record and check. 
This was selected as the best answer
Michael MMichael M
That did it- thank you
Andrew GAndrew G
Hi Michael

First, your code is not bulkified.  You have a SOQL query inside a FOR loop.  You also have a DML event (update) inside a FOR loop inside a FOR loop.  Both of these are no-nos.
Try:
public class CBOHandler {
    public static void onAfterUpdate(List<CBO__c> CBOList, Map<Id, CBO__c> oldCBOMap) {

        Set<String> cboSet = new Set<String>();
        for (CBO__c cbo : CBOList) {
            if (oldCBOMap.get(cbo.Id).Pending_for_5_min__c == false && cbo.pending_for_5_min__c == true) {
                cboSet.add(cbo.id);
            }
        }
        List<Discharge__c> disList = [SELECT id,Referrals_Status__c FROM discharge__c WHERE id IN (SELECT discharge_name__c FROM cbo__c WHERE id IN :cboSet )];
        List<Discharge__c> toUpdateList = new List<Discharge__c>();
        for (Discharge__c dis : disList){
            if (dis.Referrals_Status__c != 'Pending Outcome'){
                dis.Referrals_Status__c = 'Pending Outcome';
                toUpdateList.add(dis);
            }
        }
        if (!toUpdateList.isEmpty()){
            update toUpdateList;
        }
    }
}

Then , as noted by Poornima, you have no relationship between Discharge record and CBO record in your test class. So try:
@IsTest
public class CBOHandlerTest {
    @IsTest
    static void CboHandlerTestMethod(){
        Discharge__c d = new Discharge__c();
        d.NAME = 'Test';
        d.Referrals_Status__c='Documents Uploaded';
        insert d;

        Cbo__c c = new Cbo__c();
        c.Pending_for_5_min__c = false;
        c.Discharge__c = d.Id;  //set the relationship
        insert c;

        c = [SELECT Id, Pending_for_5_min__c from CBO__c WHERE Id =:c.Id];
        c.Pending_for_5_min__c = true;
        update c;

        c = [SELECT Id, Referrals_Status__c FROM CBO__c WHERE Id =:c.Id];
        System.assertEquals('Pending Outcome', c.Referrals_Status__c);

  }
}
 And as always, make sure we have some asserts.

Regards
Andrew
Michael MMichael M
Hi Andrew- thank you very much for those ideas on best practices. 
radd clifradd clif
Nice to see this post here and thanks for sharing this to us. Get more about the best apps here. https://yipcreations.com