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 

Help with test class on class/trigger

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
Michael MMichael M
Thank you. I was able to resolve this-- the problem was that my test class wasn't defining the parent object. So I added that c.Discharge__c (parent object) = d.id. 

All Answers

Mohamed IthrisMohamed Ithris
Hello Michael,

Create a another method and Create a new Record for Discharge object.

@isTest
Static Void AnotherCboHandlerTestMethod(){
            Discharge__c d = new Discharge__c();
            d.Name = 'Test1';
            d.Referrals_Status__c='Documents Uploaded';
                insert d;

d.Referrals_Status__c  ='Pending Outcome';
update d;
}

@isTest
Static Void againAnotherrCboHandlerTestMethod(){
            Discharge__c d = new Discharge__c();
            d.Name = 'Test2';
            d.Referrals_Status__c='Pending Outcome';
                insert d;

}

Thanks
 Mohamed Ithris
Michael MMichael M
Hi Mohamed, I just tried this, but it didn't seem to work- the class still only has 70% coverage when I run the test. 

Here is what my full test looks like that I just tested:

@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);
    
}
    
@isTest
Static Void AnotherCboHandlerTestMethod(){
            Discharge__c d = new Discharge__c();
            d.Name = 'Test1';
            d.Referrals_Status__c='Documents Uploaded';
                insert d;

d.Referrals_Status__c  ='Pending Outcome';
update d;
}

@isTest
Static Void againAnotherrCboHandlerTestMethod(){
            Discharge__c d = new Discharge__c();
            d.Name = 'Test2';
            d.Referrals_Status__c='Pending Outcome';
                insert d;

}

}
Mohamed IthrisMohamed Ithris
Hello Michel,

Trigger is writter after Update so it will call only after update.

So please add the below two lines in this method - againAnotherrCboHandlerTestMethod.

"d.Referrals_Status__c  ='Documents Uploaded';
update d;
"

@isTest
Static Void againAnotherrCboHandlerTestMethod(){
            Discharge__c d = new Discharge__c();
            d.Name = 'Test2';
            d.Referrals_Status__c='Pending Outcome';
                insert d;

d.Referrals_Status__c  ='Documents Uploaded';
update d;
}

I hope it will work.

Thanks
mohamed ithris
Michael MMichael M
Thank you. I was able to resolve this-- the problem was that my test class wasn't defining the parent object. So I added that c.Discharge__c (parent object) = d.id. 
This was selected as the best answer