+ Start a Discussion
Allen2Allen2 

There is an apex class. I have written test class on this which covered only 46% code. Could anyone please help me to cover 100% code coverage.

Apex Class

trigger updateDetails on WLog__c (after update) 
{
    List<id> caseIds = new List<id>();
    List<id> ParentCaseids = new List<id>();
    List<case> parentchildlist = new List<case>();
    List<case> updcaselist=new List<case>();
    Set<String> gbS = SMCaseUtils.getSalesOrgs('GB');

    if(trigger.isAfter && trigger.isUpdate)
    {
        for(WLog__c olog : trigger.new)        
        {
            if(olog.Result__c == 'Success' && olog.Date_Issued__c != null)
                caseIds.add(olog.Case__c);
        }
        system.debug('case ids from olog -->'+caseIds);
        if(!caseIds.isEmpty()){
            for(Case cs :[select id, ParentId, SV_Parent_Case__c from case where id in :caseIds AND Reasons__c = 'System Generated' AND Org_From_IP__c in :gbS]){
                if(cs.SV_Parent_Case__c){
                    ParentCaseids.add(cs.id);
                }else
                {
                    if(cs.parentId!=null){
                        ParentCaseids.add(cs.ParentId);
                    }else
                    {
                        ParentCaseids.add(cs.id);

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

        }
    }
}



Test class


@isTest(seeAllData=true)
public class updateDetailsTest
{
    static testMethod void ParentCaseTestMethod1()
    {
        Test.startTest();
        List<WLog__c> ologlist =[select id,Case__c,Result__c,Date_Issued__c from WLog__c limit 1];   
            if(!ologlist.isEmpty())
            {
                ologlist[0].Date_Issued__c=system.today();
                ologlist[0].Result__c='Success';
                update ologlist;
            }    
        Test.stopTest();
    }
    static testMethod void ParentCaseTestMethod()
    {
        Test.startTest();
        Set<String> gbS = SMCaseUtils.getSalesOrgs('GB');
        List<Case> cs=[select id, ParentId, SV_Parent_Case__c,CCDetails__c from case where 
        Reasons__c = 'System Generated' AND Org_From_IP__c in :gbS AND CCDetails__c!=null
        limit 1];
        if(!cs.isEmpty())
        {
            List<WLog__c> ologlist =[select id,Case__c,Result__c,Date_Issued__c from WLog__c 
            where Case__c=:cs[0].CCDetails__c limit 1];   
            if(!ologlist.isEmpty())
            {
                ologlist[0].Date_Issued__c=system.today();
                ologlist[0].Result__c='Success';
                update ologlist;
            }
        }
        Test.stopTest();
    }
}


The underlined bold italic part of the apex class not covered in the test class please help me out..
Dhanraj Poojary 18Dhanraj Poojary 18
For this part of code to be covered you can put debugs to check whether the size of the list you are passing on to your SOQL Query is greater than 0 from your test context probably (caseIds) and also the other parameters such as Reasons__c and List gbS
 if(!caseIds.isEmpty()){
            for(Case cs :[select id, ParentId, SV_Parent_Case__c from case where id in :caseIds AND Reasons__c = 'System Generated' AND Org_From_IP__c in :gbS]){
                if(cs.SV_Parent_Case__c){
                    ParentCaseids.add(cs.id);
                }else
                {
                    if(cs.parentId!=null){
                        ParentCaseids.add(cs.ParentId);
                    }else
                    {
                        ParentCaseids.add(cs.id);
                    }
                }          
            }
        }
Allen2Allen2
Not working Dhanraj. Can you show me how to add this into the test class.
Dhanraj Poojary 18Dhanraj Poojary 18
Sorry I missed a major part in your test class. Firstly the test class should not consist of (seeAllData=true). It is not a good practice.
The correct way to do it is Insert Test Data.
Insert Data for this method (SMCaseUtils.getSalesOrgs('GB'))
Insert Data for Case Object and pass the Id to Data Insertion of Wlog__c
Insert Data for WLog__c with Result__c = 'Success' and olog.Date_Issued__c != null
This Will probably give you more than 75% Coverage
If you are still not clear then I can Probably help you with the Code
Allen2Allen2
I tried but I think I am unable to understand because everytime test failures. Could you show me your code.
Dhanraj Poojary 18Dhanraj Poojary 18
Can you show me what you have tried till now. So that I can understand how much you have understood and guide you accordingly
Allen2Allen2
Sorry as the code not working , I have deleted it.
Dhanraj Poojary 18Dhanraj Poojary 18
Then probably try once more and I will surely help you.
Allen2Allen2
Thanks. I think you donot know how to write.
Dhanraj Poojary 18Dhanraj Poojary 18
Maybe I dont know how to write. But I dont think it is the proper way of responding to someone who is genuinely trying to help you understand the concepts. This forum is to help people who are stuck and are genuinely not able to solve some code problem and not for the people who are expecting others to write code on their behalf. Thanks and all the best. Maybe I am not the correct person to help you.