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
Abhilash DaslalAbhilash Daslal 

how to cover the lines of method having multiple nested if statements

Hi Guys,
Can anyone give the test method for the following

  public static List<Contact> updateContactList(Set<ID> setAccountID, Map<ID,Contact> mapContact,List<Contact> listNewContacts,List<Contact> listExistingContacts){
    try{             
        Contact newContact = new Contact();
        if(!setAccountID.isEmpty()){
            listExistingContacts = [select id,LastName,Phone from Contact where Account in : setAccountID];              
            //Update existing Contact Records        
            if(listExistingContacts != null &&  !listExistingContacts.isEmpty()){
                for(Contact existingContact :listExistingContacts){
                    newContact  = mapContact.get(existingContact.Account); 
                    if( existingContact.Required__c = true && (newContact.Account == existingContact.Account) ){
                        //No need to insert a new Contact record
                        if (listNewContacts.indexOf(newContact) !=-1 ){
                            listNewContacts.remove(listNewContacts.indexOf(newContact));                         
                        }else{}                       
                    }else if( existingContact.Required__c = true && (newContact.Account != existingContact.Account )){
                       
                        existingContact.Required__c = false;
                  }           
                }                        
            }
          } 
        }catch(Exception ex){ex.getMessage();}       
        return listExistingContacts;
    }   
Manj_SFDCManj_SFDC
Hi Abhilash,
to cover the mentioned method you need to invoke this
updateContactList(Set<ID> setAccountID, Map<ID,Contact> mapContact,List<Contact> listNewContacts,List<Contact> listExistingContacts)
from the test class, by passing the valid values, 
setAccountID - insert the Account and pass the ID
mapContact - insert the contacts and assign to the Account created in the previous step, add them to Map and while inserting the Contact do this
for eg
Contact newCon = new Contact(LastName ='test', Required__c  =true);
insert newCon;