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
MaheemSamMaheemSam 

Not able to cover map in test class

Hi,

   Below piece of code is not covering the test class please suggest me how to cover this code
 
public static void accountRollup1(Map<id, Contact> newMap, Map<id, Contact> oldMap) {
        Map<id, List<Contact>> actCntMap = new Map<id, List<Contact>>();
        set<id> oldActidSet = new set<id>();
        set<id> newActidSet = new set<id>();
        for(Contact cont : newMap.values()){
            Contact oldCont = oldMap.get(cont.id);
            if(cont.AccountId == oldCont.AccountId){
                
            }
            List<contact> conts;
            oldActidSet.add(oldCont.AccountId);
            newActidSet.add(cont.AccountId);
            if(actCntMap.containsKey(cont.AccountId)) {
                conts = actCntMap.get(cont.AccountId);
            } else {
                conts = new List<Contact>();
            }
            conts.add(cont);
            actCntMap.put(Cont.AccountId, conts);
        }

        Map<id, Account> actMap = new Map<id, Account>([select id,NSE_1_Status__c,NSE_2_Status__c,NSE_3_Status__c,
                                                         NSE_4_Status__c, NSE_5_Status__c,NSE_6_Status__c, NSE_7_Status__c,
                                                         NSE_8_Status__c from account where id in :actCntMap.keySet()]);
        Map<id, Account> actOldMap = new Map<id, Account>([select id,NSE_1_Status__c,NSE_2_Status__c,NSE_3_Status__c,
                                                         NSE_4_Status__c, NSE_5_Status__c,NSE_6_Status__c, NSE_7_Status__c,
                                                         NSE_8_Status__c from account where id in :oldActidSet]);
        
        for(ID aid : actCntMap.keySet()) {
            system.debug(aid);
            system.debug(actMap);
            Account a = actMap.get(aid);
 
            for(Contact c : actCntMap.get(aid)) {
                Contact oc = oldMap.get(c.id);
                if(c.AccountId != oc.AccountId){
                    
                }
                if(c.NSE_1__c != oc.NSE_1__c) {
                    a.NSE_1_Status__c = changeCount(a.NSE_1_Status__c, c.NSE_1__c ? 1 : -1);
                }
                if(c.NSE_2__c != oc.NSE_2__c) {
                    a.NSE_2_Status__c = changeCount(a.NSE_2_Status__c,  c.NSE_2__c ? 1 : -1);
                }
                if(c.NSE_3__c != oc.NSE_3__c) {
                    a.NSE_3_Status__c = changeCount(a.NSE_3_Status__c,  c.NSE_3__c ? 1 : -1);
                }
                if(c.NSE_4__c != oc.NSE_4__c) {
                    a.NSE_4_Status__c = changeCount(a.NSE_4_Status__c,  c.NSE_4__c ? 1 : -1);
                }
                if(c.NSE_5__c != oc.NSE_5__c) {
                    a.NSE_5_Status__c = changeCount(a.NSE_5_Status__c,  c.NSE_5__c ? 1 : -1);
                }
                if(c.NSE_7__c != oc.NSE_7__c) {
                    a.NSE_7_Status__c = changeCount(a.NSE_7_Status__c,  c.NSE_7__c ? 1 : -1);
                }
                if(c.NSE_8__c != oc.NSE_8__c) {
                    a.NSE_8_Status__c = changeCount(a.NSE_8_Status__c,  c.NSE_8__c ? 1 : -1);
                }
            }
        }
        update actMap.values();
    } 
    
    public static void updateAccountRollup(List<Contact> newLst, Integer count) {
        Map<id, List<Contact>> actCntMap = new Map<id, List<Contact>>();
        for(Contact cont : newLst){
            if(cont.AccountId == null) continue;
            List<contact> conts;
            if(actCntMap.containsKey(cont.AccountId)) {
                conts = actCntMap.get(cont.AccountId);
            } else {
                conts = new List<Contact>();
            }
            conts.add(cont);
            actCntMap.put(Cont.AccountId, conts);
        }
        Map<id, Account> actMap = new Map<id, Account>([select id,NSE_1_Status__c,NSE_2_Status__c,NSE_3_Status__c,
                                                         NSE_4_Status__c, NSE_5_Status__c,NSE_6_Status__c, NSE_7_Status__c,
                                                         NSE_8_Status__c from account where id in :actCntMap.keySet()]);
        
        for(ID aid : actCntMap.keySet()) {
            system.debug(aid);
            system.debug(actMap);
            Account a = actMap.get(aid);
            
            for(Contact c : actCntMap.get(aid)) {
                if(c.NSE_1__c) {
                    a.NSE_1_Status__c = changeCount(a.NSE_1_Status__c, count);
                }
                if(c.NSE_2__c) {
                    a.NSE_2_Status__c = changeCount(a.NSE_2_Status__c, count);
                }
                if(c.NSE_3__c) {
                    a.NSE_3_Status__c = changeCount(a.NSE_3_Status__c, count);
                }
                if(c.NSE_4__c) {
                    a.NSE_4_Status__c = changeCount(a.NSE_4_Status__c, count);
                }
                if(c.NSE_5__c) {
                    a.NSE_5_Status__c = changeCount(a.NSE_5_Status__c, count);
                }
                if(c.NSE_7__c) {
                    a.NSE_7_Status__c = changeCount(a.NSE_7_Status__c, count);
                }
                if(c.NSE_8__c) {
                    a.NSE_8_Status__c = changeCount(a.NSE_8_Status__c, count);
                }
            }
        }
            update actMap.values();
        

    }
    
    private static decimal changeCount(decimal status, Integer count){
        decimal newCount = (status == null ? 0 : status) + count;
        system.debug(newCount);
        return (newcount < 0 ? 0 : newcount);
    }

 
Best Answer chosen by MaheemSam
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Sudhir Narayanaswamy,

There are two types of Map which we can cover using test class.

1) Private Map - Map is declared as Private or inside a method which is not accessible in Test Class. We can not pass values by test class.
In order to cover private map, first, we need to find the line where we put values into a map in our class like mapName.put(). We need to focus only on this line. If put() method of Map is under any IF condition or For loop. Please make sure that IF condition and For loop is covered first then the only map will be covered.

2) Public Map - Map is declared as Public which can be accessible in Test Class. We can pass values from Test class.
In order to cover public map, we can simply access this map in our test class and put values directly it. It can also be a parameterized map which is passed inside a method like
Public Void Method_Name( Map< Id, Account > ACC );
We can put values while calling this method in our test class.

Please refer the below link for reference. I hope it will be helpful.

Please mark it as best answer if the information is informative.

Thanks
Rahul
 

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Sudhir Narayanaswamy,

There are two types of Map which we can cover using test class.

1) Private Map - Map is declared as Private or inside a method which is not accessible in Test Class. We can not pass values by test class.
In order to cover private map, first, we need to find the line where we put values into a map in our class like mapName.put(). We need to focus only on this line. If put() method of Map is under any IF condition or For loop. Please make sure that IF condition and For loop is covered first then the only map will be covered.

2) Public Map - Map is declared as Public which can be accessible in Test Class. We can pass values from Test class.
In order to cover public map, we can simply access this map in our test class and put values directly it. It can also be a parameterized map which is passed inside a method like
Public Void Method_Name( Map< Id, Account > ACC );
We can put values while calling this method in our test class.

Please refer the below link for reference. I hope it will be helpful.

Please mark it as best answer if the information is informative.

Thanks
Rahul
 
This was selected as the best answer
MaheemSamMaheemSam
Thanks Rahul for you reply,

Can you help me with the error below to fix Please suggest me to pass the map parameter 

Error: Compile Error: Method does not exist or incorrect signature: [ContactCertificateRollupHandler].accountRollup1(Contact, Contact) at line 45 column 3​
@isTest(seealldata=true)

public class ContactCertificateRollupHandlerTest{

   public static testmethod void ContactCertRollup(){ 
    
   Test.startTest();
    
   Account Act = new Account( Name = 'Test Sudhir Ac',Website='www.sudhir.com',Industry='Legal',Customer_Status__c='Current Customer',
                                 BillingStreet='894', BillingCity='sunnyvalley', BillingState='CA',BillingPostalCode='997',BillingCountry='United States');  
                     
   insert Act; 
        
  
  
   Contact cnt = new Contact(FirstName='samarth',LastName='maheem',email='sam@mahi.com',accountid=act.id,
                             NSE_1__c=true,NSE_2__c=true,NSE_3__c=true,NSE_4__c=true,NSE_5__c=true,NSE_7__c=true,NSE_8__c=true,
                             NSE_6_FortiMail__c=true,NSE_6_FortiWireless__c=true,NSE_6_FortiAuthenticator__c=true,NSE_6_FortiWeb__c=true,
                             NSE_6_FortiADC_D__c=true,NSE_6_FortiADC_E__c=true,NSE_6_FortiDDos__c=true,NSE_6_FortiSandbox__c=true,NSE_6_FortiWireless_Controller__c=true);
    
   insert cnt;
   
   Act.NSE_1_Status__c = 1;
   Act.NSE_2_Status__c = 1;
   Act.NSE_3_Status__c = 1;
   Act.NSE_4_Status__c = 1;
   Act.NSE_5_Status__c = 1;
   Act.NSE_6_Status__c = 1;
   Act.NSE_7_Status__c = 1;
   Act.NSE_8_Status__c = 1;
   
   Act.NSE_1_Required__c = 1;
   Act.NSE_2_Required__c = 1;
   Act.NSE_3_Required__c = 1;
   Act.NSE_4_Required__c = 1;
   Act.NSE_5_Required__c = 1;
   Act.NSE_6_Required__c = 1;
   Act.NSE_7_Required__c = 1;
   Act.NSE_8_Required__c = 1;
   
  update Act; 
  
  ContactCertificateRollupHandler cru = new ContactCertificateRollupHandler();
  
  cru.accountRollup1(cnt,cnt);  //error here
 
  ContactCertificateRollupHandler.changeCount(1,1);
 
   
  Test.stopTest(); 
   
   
  
   //cru.accountRollup1(cnt,cnt);
   
   }    

}

Thanks
Sudhir