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
vishwa attikerivishwa attikeri 

How to define constructor for test class

hi,

 

Please any one help me on this issue

I have a wrapper class and passing the values from main class and i am not able to write a test class for Wrapper class  please any one help me 

 

Main class Code sample

 

   public  List<CustForecastWrapper> custForecasts = null;
    public List<CustForecastWrapper> getCustForecasts(){
  
        New_Forecast__c CustFCst=new New_Forecast__c();
        
        CustFCst=[select id,name from new_forecast__c where Region__r.sales_Rep__c=:usr.id limit 1];
        
        List<Product2> ProdLst=new List<Product2>();
        Map<Id,List<Forecast_sku__c>> CustForcstMap = new Map<Id,List<Forecast_sku__c>>();
        Map<Id,List<Forecast_sku__c>> CustForcstMap1 = new Map<Id,List<Forecast_sku__c>>();
        Map<Id,List<Forecast_sku__c>> CustForcstMapLastYear = new Map<Id,List<Forecast_sku__c>>();
        Map<Id,List<Forecast_sku__c>> CustForcstMapLtLastYear = new Map<Id,List<Forecast_sku__c>>();
        Map<Id,Product2> ProdMap = new Map<Id,Product2>();
        List<Forecast_sku__c> ForcastSKULst = new List<Forecast_sku__c>();
          List<Forecast_sku__c> ForcastSKULst1 = new List<Forecast_sku__c>();
        
        Map<Id,new_forecast__c> CustForcstRecMap = new Map<Id,new_forecast__c>();
        
        ProdLst=[SELECT Id, Name FROM Product2 WHERE IsActive = true order by name];
        for(Product2 Prod : ProdLst){
         List<Forecast_sku__c> TempForcst11 = new List<Forecast_sku__c>();
            CustForcstMap1.put(Prod.Id,TempForcst11);
            
            List<Forecast_sku__c> TempForcst = new List<Forecast_sku__c>();
            CustForcstMap.put(Prod.Id,TempForcst);
            
            List<Forecast_sku__c> TempForcst1 = new List<Forecast_sku__c>();
            CustForcstMapLastYear.put(Prod.Id,TempForcst1);         
            
            List<Forecast_sku__c> TempForcst2 = new List<Forecast_sku__c>();
            CustForcstMapLtLastYear.put(Prod.Id,TempForcst2);  
            ProdMap.put(Prod.Id,Prod);
        }
        integer lyear = integer.valueOf(year)-1;
        Integer LtLYear = Integer.valueOf(year)-2;
        string LastYear = string.valueOf(lyear);
        string LtLastYear = string.valueOf(LtLYear);
        
        
        ForcastSKULst = [SELECT Year__c ,sku__c,New_Forecast__r.product__c,Actual_Amount__c,Budget_Amount__c,Plan_Amount__c,Sample_Amount__c,Month__c,Unit_Price__c,Net_price__c,Sample_Quantity__c,Discount__c,Actual_Quantity__c,Budget_Quantity__c, Plan_Quantity__c FROM Forecast_sku__c WHERE  new_forecast__r.Region__r.sales_Rep__c=:usr.id AND sku__c IN:ProdLst AND (Year__c =:year OR Year__c =:LastYear OR Year__c=:LtLastYear)];
        
        ForcastSKULst1 = [SELECT Year__c ,sku__c,New_Forecast__r.product__c,Actual_Amount__c,Budget_Amount__c,Plan_Amount__c,Sample_Amount__c,Month__c,Unit_Price__c,Net_price__c,Sample_Quantity__c,Discount__c,Actual_Quantity__c,Budget_Quantity__c, Plan_Quantity__c FROM Forecast_sku__c WHERE  new_forecast__r.Customer__r.Region1__r.sales_Rep__c=:usr.id AND sku__c IN:ProdLst AND (Year__c =:year OR Year__c =:LastYear OR Year__c=:LtLastYear)];
            
            
         for(Forecast_sku__c oForcst : ForcastSKULst1){
if(CustForcstMap1.containsKey(oForcst.sku__c) && oForcst.Year__c==year){
                CustForcstMap1.get(oForcst.sku__c).add(oForcst);
            }           
        } 
        
        
        for(Forecast_sku__c oForcst : ForcastSKULst){
            if(CustForcstMap.containsKey(oForcst.sku__c) && oForcst.Year__c==year){
                CustForcstMap.get(oForcst.sku__c).add(oForcst);
            }           
        }        
        for(Forecast_sku__c oForcst : ForcastSKULst){
            if(CustForcstMapLastYear.containsKey(oForcst.sku__c) && oForcst.Year__c==LastYear){             
                CustForcstMapLastYear.get(oForcst.sku__c).add(oForcst);
            }
        }
        for(Forecast_sku__c oForcst : ForcastSKULst){
            if(CustForcstMapLtLastYear.containsKey(oForcst.sku__c) && oForcst.Year__c==LtLastYear){             
                CustForcstMapLtLastYear.get(oForcst.sku__c).add(oForcst);
            }
        }
        
        Map<Id,double> PriceMap = new Map<Id,double>();
        PricebookEntry[] PricEntry = [Select UnitPrice,Product2Id From PricebookEntry where Product2Id IN:ProdMap.KeySet()];
        for(PricebookEntry oPB : PricEntry){
            PriceMap.put(oPB.Product2Id,oPB.UnitPrice);
        }
        
        for(Id oId : CustForcstMap.KeySet()){
            CustForecastWrapper oProdFrcstWrap = new CustForecastWrapper(ProdMap.get(oId), Year ,CustFCst,CustForcstMap1.get(oId),CustForcstMap.get(oId),CustForcstMapLastYear.get(oId), CustForcstMapLtLastYear.get(oId));
            if(PriceMap.containsKey(oID)){
                oProdFrcstWrap.skprc = String.ValueOF(PriceMap.get(oID));
                if(oProdFrcstWrap.skprc=='')
                    oProdFrcstWrap.skprc = 'Please add Standard Price to SKU';
            }           
            custForecasts.add(oProdFrcstWrap);            
        }
        
        return custForecasts;
    }

 

and This is my wrapper class

 

public class CustForecastWrapper{

   
    
    public CustForecastWrapper(Product2 prod, String year,new_forecast__c nfc1,List<Forecast_sku__c> LFSKU11,List<Forecast_sku__c> LFSKU,List<Forecast_sku__c> LFSKULstYear,List<Forecast_sku__c> LFSKUSecLstYear ){
        
        
    
}

 

Thanks....

Shashikant SharmaShashikant Sharma

You don't need to write separate test method to test the wrapper class , intead your test method for you class should be like that it covers your wrapper. Text Context fuctional flow should be like end user functional flow.

 

If you will create a test method so that MAP CustForcstMap gets filled , it will automatically cover your wrapper.

for(Id oId : CustForcstMap.KeySet()){

 Let me know if any issue in it.