You need to sign in to do that
Don't have an account?

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);
}
}
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);
}
}
All Answers
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:
Then , as noted by Poornima, you have no relationship between Discharge record and CBO record in your test class. So try: And as always, make sure we have some asserts.
Regards
Andrew