+ Start a Discussion
murdocmurdoc 

Test Class Replication

Hola homies,

I inherited a class that is mired in nonsense and I need help writing a test class for a particular method. If I get it right I can replicate the test class and significantly raise the coverage on this class. HELP! Please.Here is the method. Thanks in advance!
public static List<Task> returnUpdateTaskListOwner(List<Opportunity> updateOpprtunityOwner,Map<Id, List<Task>> oppTaskMap,Map<Id,Opportunity> oldOppMap){
        List<Task> tempListUpdate=new List<Task>();
        if(updateOpprtunityOwner != null && updateOpprtunityOwner.size() > 0){
                    for(Opportunity opp : updateOpprtunityOwner){
                        if(oppTaskMap.containsKey(opp.Id)) {
                            List<Task> tempList=oppTaskMap.get(opp.Id);
                                for(Task t : tempList){//I-155449 Changes to check task subject
                                        System.debug('>>>task>>'+t.Subject);
                                    if(t.OwnerId == oldOppMap.get(opp.Id).OwnerId && t.Status != 'Completed' && taskWithOwner.contains(t.Subject)){
                                        System.debug('>>>>>updatetaskwithid'+t.id);
                                        t.OwnerId=opp.OwnerId;
                                        tempListUpdate.add(t);
                                    }
                                }
                        }
                    }
                }
            return tempListUpdate;
    }

 
Best Answer chosen by murdoc
Deepali KulshresthaDeepali Kulshrestha
Hi Murdoc,

Try the following test-class, it may be helpful for you:
@IsTest
public class returnUpdateTaskListOwner_Test {
    @IsTest
    public static void UpdateTaskTestMethod(){
        List<Opportunity> oppList = new List<Opportunity>(); 
        Opportunity opp=new Opportunity();
        opp.Name = 'test opp 1';
        opp.StageName = 'Qualification';
        opp.CloseDate = system.today(); 
        oppList.add(opp);
        insert oppList;
        
        List<task> tList = new List<task>();
        task t=new task();
        t.WhatID = oppList[0].id;
        t.Subject='Donni';
        t.Status='In Progress';
        t.Priority='Normal';
        tList.add(t);
        insert tList;  
        Map<Id, List<Task>> oppTaskMap=new Map<Id, List<Task>>();
        oppTaskMap.put(oppList[0].Id,tList);
        Map<Id,Opportunity> oldOppMap=new Map<Id,Opportunity>();
        oldOppMap.put(oppList[0].Id,oppList[0]);
        returnUpdateTaskListOwner.returnUpdateTaskListOwner_Method(oppList,oppTaskMap,oldOppMap);
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha