-2

I am taking a reference from the link: Can we merge if else condition in a single test class method?. I have a code something like below and I want to write the test class for it.

Map<Id, Case> oldCasesMap = (Map<Id, Case>)oldSobjsMap;
if(cs.Category__c == 'Basic Authorization'){
    if(cs.SubCategory == 'Auth.Net'){                     
        if(oldCasesMap != null){
            Case oldCs = oldCasesMap.get(cs.Id);                               
                if(cs.Status == 'XXXX' && cs.SubStatus == 'YYYY' && 
                    oldCs.Status == 'MMMM' && oldCs.SubStatus == 'NNNN'){
                        caseIds.add(cs.Id);
                        MilestoneUtils.completeMilestone(caseIds, milestoneNames, System.now());
                    }else if(cs.Status == 'OOOO' && cs.SubStatus == 'LLLL'){
                        caseIds.add(cs.Id);
                        MilestoneUtils.completeMilestone(caseIds, milestoneNames, System.now());
                    }
                }
            }   
        }

I am not sure why the test class not covering the If part. I have written a test method like below.

public static testMethod void testLabelReviewRequest(){
        Test.startTest();
        Account acc = new Account(Name='Test');
        insert acc;

        Id entProcessId = [SELECT Id FROM SlaProcess WHERE SObjectType = 'Case' 
                                   AND Name LIKE '%XXXXXX%' AND IsActive = true LIMIT 1].Id;

        Entitlement ent = new Entitlement(Name='Sample Name', AccountId =acc.Id,
                                        SLAProcessId = entProcessId, Type='Phone Support');
        insert ent;

        Case c = new Case();
        c.RecordTypeId = '123456';
        c.Category = 'QQQQ';
        c.SubCategory = 'WWWW';
        ...........
        ...........

        List<Case> cases = new List<Case>();
        cases.add(c.clone(false));
        ...............
        insert cases;

        cases[0].Status = 'Pending';
        cases[0].SubStatus = 'LLLL';
        cases[1].Status = 'In Progress';
        cases[1].SubStatus = 'KKKKK';
        cases[2].Status = 'In Progress';
        cases[2].SubStatus = 'GGGG';
        cases[3].Status = 'CCCC';
        update cases;          
        Test.stopTest();
    }

What modifications do I need to make in order to cover the oldMap code?

0

After inserting the cases, query the list of of cases and then assign the values and finally update.

Create the values which will satisfy the conditions.

Entitlement ent = new Entitlement(Name='Sample Name', AccountId =acc.Id,
                                        SLAProcessId = entProcessId, Type='Phone Support');
insert ent;

Case c = new Case();
c.RecordTypeId = '123456';
c.Category = 'QQQQ';
c.SubCategory = 'WWWW';
c.EntitlementId = ent.Id;
c.Status = 'MMMM';
c.SubStatus = 'NNNN';
...........
...........

List<Case> cases = new List<Case>();
cases.add(c.clone(false));
...............
Database.SaveResult[] result = database.insert(cases);
Set<id> Ids = new Set<Id>();
for(Database.SaveResult sr : srList)
{
    Ids.add(sr.getId());
}
List<Case> lstCase = [SELECT Status,SubStatus FROM Case WHERE Id IN:Ids]; 

lstCase[0].Status = 'Pending';
lstCase[0].SubStatus = 'LLLL';
lstCase[1].Status = 'In Progress';
lstCase[1].SubStatus = 'KKKKK';
lstCase[2].Status = 'In Progress';
lstCase[2].SubStatus = 'GGGG';
lstCase[3].Status = 'CCCC';
update lstCase;          
Test.stopTest();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.