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
Makam RuthvikMakam Ruthvik 

How to write test class for the map

public Map<String,Object> con018{
        get{
            if(AccountId!=null){
                Map<String,Object> linkMap = new Map<String,Object>();
                  List<Account> or1 = [SELECT Id,Parent.Id FROM Account WHERE Id =:AccountId];
                List<Account> or2 = [SELECT Id,AccountReferenceNumber__c, (SELECT vlocity_cmt__DueDate__c,BillToContact.FirstName,SNL_ActualStartDate__c,Usage_Monthly_Total_Including_VAT__c  FROM orders) FROM Account WHERE Id =:or1[0].Parent.Id];

                         list<Order> OrderList = new List<Order>(); 
String ff;
                for(Account a : or2)
        {
           for(Order c : a.Orders)
            {
               OrderList.add(c);     //  assign contact to the list
            }
        }
        for(Order z : OrderList){
     ff=z.BillToContactId;
}
                        List<Contact> Con=[Select Firstname from Contact where Id=:ff];

                
                
                String refno='refno';
                String refnoval = String.valueOf(or2[0].get('AccountReferenceNumber__c'));
                linkMap.put(refno,refnoval);
                String fname = 'fname';
                String fnameval = String.valueOf(Con[0].get('FirstName'));
                linkMap.put(fname,fnameval);
                String asd = 'asd';
                DateTime asdval = OrderList[0].SNL_ActualStartDate__c;
                linkMap.put(asd,asdval);
                String amnt = 'amnt';
                String amntval = String.valueOf(OrderList[0].Usage_Monthly_Total_Including_VAT__c);
                linkMap.put(amnt,amntval);
                String dd = 'dd';
                DateTime ddval = OrderList[0].vlocity_cmt__DueDate__c;
                linkMap.put(dd,ddval);
                
                
                return linkMap;
            }else{
                return null;
            }
              

        }
        set;
    }
ShivankurShivankur (Salesforce Developers) 
Hi Ruthvik,


First of all, we need to identity which kind of Map we are using in our class.

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 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 only map will be covered.

2) Public Map - Map is declared as Public which can be accessible in Test Class. We can pass values by 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.

For more guidance on writing test classes:
https://www.infallibletechie.com/2018/08/salesfore-apex-test-class-best-pracitces.html

Hope above information helps. Please mark as Best Answer so that it can help others in future.

​​​​​​​Thanks.
Suraj Tripathi 47Suraj Tripathi 47

Hi Please find the solution.

Do the needful changes.

public testMethod static void con018Test(){

 User u = new User(Alias = 'test', Email='TestData@testorg.com', 
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='TestData@testorg.com');
			insert u;
			
			 

   Account a=new Account();
   a.Name='Test';
   a.ParentId=u.id;
   a.AccountReferenceNumber__c='Check Data';
   insert a;
   
  

   
    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    
    

    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
    
    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = standardPb.Id;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    //Test Order Insert
    
    Order o = new Order();
	o.SNL_ActualStartDate__c=datetime.now();
	o.Usage_Monthly_Total_Including_VAT__c='Data';
	o.vlocity_cmt__DueDate__c=datetime.now();
    o.Name = 'Test Order ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =  standardPb.Id ;
    
    insert o;
   
    
	Contact con=new Contact();
			con.LastName='Test Data';
			con.orderid=o.id;
	        con.Name='Bill';
	        con.FirstName='Test Data';
 			insert con;

   
   Test.startTest();
   className.AccountId=a.id;
   
   className.con018();
   
   Test.stopTest();

}


Please mark it as the Best Answer so that other people would take reference from it.

Thank You

Makam RuthvikMakam Ruthvik
Hello SUraj,

It is showing the following error.

"Method does not exist or incorrect signature: void con018() from the type Art_Order" here Art_Order is my controller
Suraj Tripathi 47Suraj Tripathi 47

hi

Are you still looking for answer?