+ Start a Discussion
Allen2Allen2 

written a test class for the following apex class. Not able to total coverage of apex class. I m not getting what is wrong. Please could anyone help me out to solve this.....

APEX CLASS


trigger updatePtCDetails on OtB__c (after update) 
{
    List<id> caseIds = new List<id>();
    List<id> PCaseids = new List<id>();
    List<case> prntchildlist = new List<case>();
    List<case> updcaselist=new List<case>();
    Set<String> gbS = SMCUtils.getSOrgs('G P');

    if(trigger.isAfter && trigger.isUpdate)
    {
        for(OtB__c B : trigger.new)        
        {
            if(B.Result__c == 'Success' && B.Dt_Issued__c != null)
                caseIds.add(B.Case__c);
        }
        system.debug('case ids from B -->'+caseIds);
        if(!caseIds.isEmpty()){
            for(Case cs :[select id, PtId, SX_Is_Pt_Case__c from case where id in :caseIds AND Reasons__c = 'System Generated' AND S_OG__c in :gbS]){
                if(cs.SX_Is_Pt_Case__c){
                    PCaseids.add(cs.id);
                }else
                {
                    if(cs.prntId!=null){
                        PCaseids.add(cs.PtId);
                    }else
                    {
                        PCaseids.add(cs.id);

                    }
                }          
            }
        }
        system.debug('parent ids list -->'+PCaseids);   
        if(!PCaseids.isEmpty()){
            prntchildlist=[select id,Reasons__c,C_Status__c,(select id,C_Completed_On__c from Cases) from case where id in :PCaseids];
        }
        system.debug('parent child list -->'+prntchildlist);
        for(Case parent : prntchildlist)
        {
            Boolean isDateNull=false;
            for(Case child : parent.cases)
            {
                if(child.C_Completed_On__c==null)
                {
                    isDateNull=true;
                    break;
                }
            }
            if(isDateNull==false)
            {
                parent.C_Status__c='Process Complete';
                updcaselist.add(parent);
            }   
        }
        if(!updcaselist.isEmpty())
        {
            system.debug('inside update for case list in B-->'+updcaselist);
            update updcaselist;
        }

    }
}


TEST CLASS


@isTest
public class updatePtCDetailsTest
{
    static testMethod void TestMethod1()
    {
        Test.startTest();
        List<OtB__c> otlist =[select id,Case__c,Result__c,Dt_Issued__c from OtB__c limit 1];   
            if(!ologlist.isEmpty())
            {
                otlist[0].Dt_Issued__c=system.today();
                otlist[0].Result__c='Success';
                update otlist;
            }    
        Test.stopTest();
    }
    static testMethod void TestMethod()
    {       
        Test.startTest();
        Set<String> gbS = SMCUtils.getSOrgs('G P');
        List<Case> cs=[select id, PtId, SX_Is_Pt_Case__c,CCDetails__c from case where Reasons__c = 'System Generated' AND S_OG__c in :gbS AND CCDetails__c!=null
        limit 1];
        if(!cs.isEmpty())
        {  
            List<OtB__c> otlist =[select id,Case__c,Result__c,Dt_Issued__c from OtB__c 
            where Case__c=:cs[0].CCDetails__c limit 1];
            if(!otlist.isEmpty())
            {
                otlist[0].Dt_Issued__c = null;
                otlist[0].Result__c='Success';
                update otlist;
            }
        }
        Test.stopTest();
    }
}

The bold and underlined part is not able to cover. please help me out for this.. urgent...
Ganesh Hembram 21Ganesh Hembram 21
Hi Chhotumotu,

Could you please check your test class, what value you are getting in line 7
List<OtB__c> otlist =[select id,Case__c,Result__c,Dt_Issued__c from OtB__c limit 1];

And you have not even created the dummy/sample data for your test class.

First of all create some dummy/sample data and fullfill the criteria based on the class. Below are the link for reference
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
https://trailhead.salesforce.com/modules/apex_testing/units/apex_testing_intro

Thanks & Regards,
Ganesh Hembram
Allen2Allen2
Hi Ganesh,
Only the testmethod1 is covering all the code testmethod is not covering anything. Just help me how to cover if else part in this...
Ganesh Hembram 21Ganesh Hembram 21
Hi Chhotumotu,

Could you please try the below code snippet for testmethod once and let me know..
static testMethod void TestMethod(){       
        Test.startTest();
        Set<String> gbS = SMCUtils.getSOrgs('G P');
		List<OtB__c> oTBList =[select id,Case__c,Result__c,Dt_Issued__c from OtB__c limit 1];
		if(!oTBList.isEmpty()) {
                oTBList[0].Dt_Issued__c=system.today();
                oTBList[0].Result__c='Success';
        }
		update oTBList;
		
        List<Case> cs=[select id, PtId, SX_Is_Pt_Case__c,CCDetails__c from case where Id IN : oTBList[0].Case__c AND Reasons__c = 'System Generated' 
					   AND S_OG__c in :gbS limit 1];
        Test.stopTest();
    }

Thanks & Regards,
Ganesh Hembram
 
Allen2Allen2
Hi Ganesh,

Getting this error in "IN operator must be used with an iterable expression" while creating "list<case> " . we can't use Id In: oTBList[0].case__c.
M not able to solve this.
 
Ganesh Hembram 21Ganesh Hembram 21
static testMethod void TestMethod(){       
	Test.startTest();
	Set<String> gbS = SMCUtils.getSOrgs('G P');
	Set<Id> caseId = Set<Id>();
	
	List<OtB__c> oTBList =[select id,Case__c,Result__c,Dt_Issued__c from OtB__c limit 1];
	if(!oTBList.isEmpty()) {
			oTBList[0].Dt_Issued__c=system.today();
			oTBList[0].Result__c='Success';
	}
	update oTBList;
	
	for(OtB__c oTB : oTBList) {
		caseId.add(oTBList.Case__c);
	}
	
	List<Case> cs=[select id, PtId, SX_Is_Pt_Case__c,CCDetails__c from case where Id IN : caseId AND Reasons__c = 'System Generated' AND S_OG__c in :gbS limit 1];
	Test.stopTest();
}

 
Allen2Allen2
These are the errors...

Set , Id variable not exist for line 4, Case__c  variable not exist  for line 14, Id variable not exist for line 17.