function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Miguel RoaMiguel Roa 

Help writing a kinda complex test class

Hello guys, I'm really new to APEX/Salesforce and I've been told to write a test class.

The thing is that all the examples that I see online are for really simple classes and this one is kinda complicated.

Can someone help me with this or point me in the right direction?

Thanks in advance.

This is the class:
 
public without sharing class OpportunityUpdate_WithDVSSCaseHelper {
    
    public static void UpdateOpptyStatusForOpptyWithDVSSCases_OnPegaSide(List<Opportunity> newOppty,Map<Id,Opportunity> oldOppty){
      
        Map<String,String> DvssCaseTypes = new Map<String,String>();
        DvssCaseTypes.put('DVSS AM', 'DVSSAM'); 
        DvssCaseTypes.put('DVSS CPE','DVSSCPE');
        DvssCaseTypes.put('DVSS DR','DVSSDR');
        DvssCaseTypes.put('DVSS RFP','DVSSRFP');
    
        List<Id> opptyIds = new List<Id>();
        List<Opportunity> opptiesToUpdateOnPega;   
        
        List<WrapperDVSSOpptyStageUpdateService> wrappers = new List<WrapperDVSSOpptyStageUpdateService>();
        for(Opportunity oppty:newOppty){
            
            Opportunity oldOp = oldOppty.get(oppty.Id);
            if(oppty.StageName != oldOp.StageName && (oppty.StageName =='5 Closed Lost' || oppty.StageName =='5 Closed Disqualified' ) ){
                opptyIds.add(oppty.Id);
            }
            
        }   
        
        
        if(opptyIds.size() > 0){

            opptiesToUpdateOnPega = [select Id,StageName,(SELECT Case_Type__c,SWAP_Case_ID__c FROM SWAPS_Tracker__r where case_type__c in ('DVSS AM','DVSS CPE','DVSS DR','DVSS RFP')) 
                                     from Opportunity where Id in:opptyIds and Id in(select Opportunity_Name__c from SWAP_Tracker__c
                                                                                     where Opportunity_Name__c in:opptyIds and case_type__c in ('DVSS AM','DVSS CPE','DVSS DR','DVSS RFP')) ];

           if(opptiesToUpdateOnPega.size() > 0){
                
                for(Opportunity opp:opptiesToUpdateOnPega){
                    
                    List<SWAP_Tracker__c> swTrack = opp.SWAPS_Tracker__r;
                    
                    system.debug('Oppt info LP>> ' + opp);
                    DateTime dtvar = null;
                    String Casetype='' ;
                    String CaseId='';
            
                    string OpptyStatus = opp.StageName;
                    
                        for(SWAP_Tracker__c swt:swTrack){
                            
                            Casetype =  (DvssCaseTypes.get(swt.Case_Type__c) == null)?'':DvssCaseTypes.get(swt.Case_Type__c);
                            CaseId = (swt.SWAP_Case_ID__c ==null)?'':swt.SWAP_Case_ID__c;
                            
                            WrapperDVSSOpptyStageUpdateService wp = new WrapperDVSSOpptyStageUpdateService();
                            wp.Casetype =  Casetype;
                            wp.CaseId = CaseId;
                            wp.OpptyStatus = OpptyStatus;
                            wrappers.add(wp);
                        }
                    }
                
                DVSSOpptyStageUpdateService updateServicePega = new DVSSOpptyStageUpdateService();
                updateServicePega.lst = wrappers;
        
                system.enqueueJob(updateServicePega);
            }
        }
    }
        
    public class DVSSOpptyStageUpdateService implements Queueable, Database.AllowsCallouts {
        
        public List<WrapperDVSSOpptyStageUpdateService> lst{get;set;}
        
        public void execute(QueueableContext context){
            
            system.debug('LL- Calling to update oppty stage');
            
            try{
                
                for(WrapperDVSSOpptyStageUpdateService wrp : lst){
                   
                        Pega_CaseMiddleware.PegaCaseResult pagecaserslt = Pega_CaseMiddleware.UpdateDVSSCases_OpptyStatus(wrp.Casetype,wrp.CaseId,wrp.OpptyStatus);
                        
                        If(pagecaserslt.Error.size()>0){                
                            
                            system.debug('LL- error Message>>> ' + pagecaserslt.Error);
                        }
                        else{
                            
                            system.debug('LL- Success Message>>> ' + pagecaserslt.CaseID);
                        } 
                }
            }
            catch(Exception ex){
                
                system.debug('LL-Errrrror: ' + ex.getMessage()+ ' -- ' + ex.getStackTraceString() + '-- ' + ex.getLineNumber());
                insertLog('outbound', '-1', ex.getMessage()+ ex.getStackTraceString() + ex.getLineNumber(), 'DVSSOpptyStageUpdateService');    
            }
        }
    }
    
    public class WrapperDVSSOpptyStageUpdateService {
        
        public string Casetype{get;set;}
        public string CaseId{get;set;}
        public string OpptyStatus{get;set;}
    }
    
    public static void insertLog(String inboundOutbound, string requestID, string message, string method){
        
        try {
            
            Event_Log__c log = new Event_Log__c();
            
            log.Record_Id__c = requestID;
            log.Operation__c = inboundOutbound;
            log.sObject_Type__c ='SWAP Case';
            log.Message__c = 'pega_salesforce_inbound_outbound';
            log.Line_Number__c = method;
            log.Notes__c = message;
            
            insert log;
        }
        catch (Exception ex) {
            System.debug(ex.getMessage());
        }
    }
}

 
SwethaSwetha (Salesforce Developers) 
HI Miguel,
The code provided in question does not highlight the uncovered lines. Since the code requires an understanding of your implementation, it might not be possible to provide exact suggestions. However, the below articles give a good insight into how coverage can be improved
 
https://salesforce.stackexchange.com/questions/244794/how-do-i-increase-my-code-coverage-or-why-cant-i-cover-these-lines 
 
Examples of test class for Queueable Apex that makes call outs:
https://salesforce.stackexchange.com/questions/312303/implementing-test-class-for-queueable-apex-that-makes-call-outs
https://salesforce.stackexchange.com/questions/276507/mock-test-for-callouts-in-a-queuable-function
 
Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you