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
ERP TeamERP Team 

Class Test for Custom Controller Extension

Hi ,

I am new to Apex I managed to create a Visualforce Page and a custom controller extension but I am unable to create a Test Class for it

Can anyone help me create the Test class please?

Controller Code : 
public class AccountSummaryController {     
    public List<OppProductWrapper> IAM {get;set;}
    public List<OppProductWrapper> IPBI {get;set;}
    public List<OppProductWrapper> Consulting {get;set;}
    public List<OppProductWrapper> Annuity {get;set;}
    public List<OppProductWrapper> Innovation {get;set;}
    public List<OppProductWrapper> Other {get;set;}
    public List<OppProductWrapper> Preparation {get;set;}
    public List<OppProductWrapper> Opswrap {get;set;}
    public List<OppProductWrapper> Priorart {get;set;}
    public List<OppProductWrapper> ContractsIAM {get;set;}
    public List<OppProductWrapper> ContractsIPBI {get;set;}
    public List<OppProductWrapper> ContractsInnovation {get;set;}
    public Contract Con {get;set;}
    public Task NewTask {get;set;}
    public Boolean ShowTask {get;set;}
    
    public OppProductWrapper Opwrap {get;set;}
    
    public List<opportunity> opportunities{get;set;} 
    
    public List<Task> Task_IAM {get;set;}
	public List<Task> Task_IPBI {get;set;}
	public List<Task> Task_Annuity {get;set;}
	public List<Task> Task_Consulting {get;set;}
	public List<Task> Task_Innovation {get;set;}
	public List<Task> Task_Other {get;set;}
	public List<Task> Task_Preparation {get;set;}
   
    public Account accounts {get;set;} 
    public Account acc {get;set;} 

    public AccountSummaryController(ApexPages.StandardController controller) { 
        acc = (account)controller.getRecord();      
	    accounts = [SELECT id FROM account WHERE id=: acc.id LIMIT 1];
        createTaskRecord() ;
		showTask = False;
        /* Get all the opportunites and their products related to the Account */
	    opportunities = [SELECT id,Name,NextStep,Existing_Solution_Name__c,Other_Solutions_Evaluated__c, CloseDate,StageName,ContractId,(SELECT Id,PricebookEntry.Product2.Product_Category__c,PricebookEntry.Product2.Product_Sub_Family__c, PricebookEntry.Product2.Family,PricebookEntry.Product2.Name FROM OpportunityLineItems)  FROM Opportunity WHERE accountid = :accounts.id AND HasOpportunityLineItem = TRUE  ORDER BY CloseDate DESC];   
        /* Parse all the infos needed in the Opportunity Product Wrapper */
        
        Annuity = new List<OppProductWrapper>();
        Innovation = new List<OppProductWrapper>();
        Other = new List<OppProductWrapper>();
        IPBI = new List<OppProductWrapper>();
		Consulting = new List<OppProductWrapper>();
		IAM = new List<OppProductWrapper>();
		Preparation = new List<OppProductWrapper>();
        Priorart = new List<OppProductWrapper>();
        ContractsIAM = new List<OppProductWrapper>();
        ContractsIPBI = new List<OppProductWrapper>();
        ContractsInnovation = new List<OppProductWrapper>();
        
        Task_IAM = [Select Id,Owner.Name,Priority,ActivityDate	,Subject,Description,Product_Family__c FROM Task where WhatId =: accounts.id and Product_Family__c ='IAM' ];
        Task_IPBI = [Select Id,Owner.Name,Priority,ActivityDate	,Subject,Description,Product_Family__c FROM Task where WhatId =: accounts.id and Product_Family__c ='IPBI'];
        Task_Annuity = [Select Id,Owner.Name,Priority,ActivityDate	,Subject,Description,Product_Family__c FROM Task where WhatId =: accounts.id and Product_Family__c ='Annuity and renewals'];
     	Task_Innovation = [Select Id,Owner.Name,Priority,ActivityDate	,Subject,Description,Product_Family__c FROM Task where WhatId =: accounts.id and  Product_Family__c ='Innovation SaaS'];
        Task_Consulting = [Select Id,Owner.Name,Priority,ActivityDate	,Subject,Description,Product_Family__c FROM Task where WhatId =: accounts.id and  Product_Family__c ='Consulting'];
        Task_Preparation = [Select Id,Owner.Name,Priority,ActivityDate	,Subject,Description,Product_Family__c FROM Task where WhatId =: accounts.id and  Product_Family__c ='Preparation and Prosecution Services' ];
        Task_Other = [Select Id,Owner.Name,ActivityDate,Priority,Subject,Description,Product_Family__c FROM Task where WhatId =: accounts.id and Product_Family__c ='Other Services' ];
        Con = New Contract();
        String indexAsset='';
               String indexIdea='';
               String indexData='';
        
           for (Opportunity opp :opportunities ) {  
                Integer indexIAM=1;
                Integer indexIPBI=1;
                Integer indexInnovation=1;
                Integer indexAnnuity=1;
                Integer indexConsulting=1;
                Integer indexPreparation=1;
                Integer indexPriorart=1;
                Integer indexOther=1;
               
               
            for (OpportunityLineItem opline: opp.OpportunityLineItems)
            {	
                Opwrap = new OppProductWrapper();
              Opwrap.OppId = opp.Id;
               if(opp.ContractId!=null){
                    Opwrap.Contract = opp.ContractId;
             		 Con = [Select Name,StartDate,EndDate,Status from Contract Where Id =: opp.ContractId limit 1];
                     Opwrap.StartDate = Con.StartDate;
                     Opwrap.EndDate = Con.EndDate;
					 Opwrap.ContractStatus = Con.Status;
                   Opwrap.ContractName = Con.Name;
               }
                
               		Opwrap.ProductName = opline.PricebookEntry.Product2.Name;
                    Opwrap.ProductFamily = opline.PricebookEntry.Product2.Family;
                    Opwrap.ProductSubFamily = opline.PricebookEntry.Product2.Product_Sub_Family__c;
                    Opwrap.ProductCategory = opline.PricebookEntry.Product2.Product_Category__c;
             	    Opwrap.Stage = opp.StageName;
            		Opwrap.CloseDate = opp.CloseDate;
             		Opwrap.Competitor = opp.Other_Solutions_Evaluated__c;
             		Opwrap.Existing = opp.Existing_Solution_Name__c;
             		Opwrap.OppName = opp.Name;
             		Opwrap.NextStep=opp.NextStep;
                
                 IF (Opwrap.Contract!=null){
                    If(opline.PricebookEntry.Product2.Family=='IAM'&& indexAsset!= Opwrap.Contract) 
                    { ContractsIAM.add(Opwrap);
                        indexAsset =Opwrap.Contract; 
                   }
                                        
                    If(opline.PricebookEntry.Product2.Family=='IPBI' && indexData!= Opwrap.Contract) 
                    { ContractsIPBI.add(Opwrap);
                        indexData =Opwrap.Contract;
                    }   
                    If(opline.PricebookEntry.Product2.Family=='Innovation SaaS'&& indexIdea!= Opwrap.Contract) 
                    { ContractsInnovation.add(Opwrap);
                        indexIdea =Opwrap.Contract;
                    }		
                }       
                Else {
             If(opline.PricebookEntry.Product2.Family=='IAM' && indexIAM<2) 
             {Opwrap.index = indexIAM;
                 IAM.add(Opwrap);
             indexIAM ++;
              
             }
             If(opline.PricebookEntry.Product2.Family=='IPBI' && indexIPBI<2) 
            { 
                Opwrap.index = indexIPBI;
                IPBI.add(Opwrap); 
             indexIPBI ++;
            }
              If(opline.PricebookEntry.Product2.Family=='Innovation SaaS' && indexInnovation<2) 
             {
                 Opwrap.index=indexInnovation;
                 Innovation.add(Opwrap); 
              indexInnovation ++;
             }
          	  If(opline.PricebookEntry.Product2.Family=='Annuity and renewals' && indexAnnuity<2) 
             {
                 Opwrap.index=indexAnnuity;
                 Annuity.add(Opwrap);
              indexAnnuity ++;
             }
              If(opline.PricebookEntry.Product2.Family=='Consulting' && indexConsulting<2) 
             {
                 Opwrap.index=indexConsulting;
                 Consulting.add(Opwrap);
              indexConsulting ++;
             }
             If(opline.PricebookEntry.Product2.Product_Sub_Family__c=='Prior-art search' && indexPriorart<2) 
            {
                Opwrap.index= indexPriorart;
                Priorart.add(Opwrap);
             indexPriorart ++;
            }
                If(opline.PricebookEntry.Product2.Family=='Other Services' && indexOther<2 && opline.PricebookEntry.Product2.Product_Sub_Family__c!='Prior-art search' ) {
                    Opwrap.index= indexOther;
                    Other.add(Opwrap);
                      indexOther ++;
                     }
                }
            }               
        }
 
    }
    public void CreateTask(){
        
        showTask=True;
    }
    public Pagereference newTask(){
        
                    try {
                            insert NewTask;
                            } 
        			catch(DMLException e) {
                            NewTask.addError(e.getMessage());
                            return null;
                            }
        
  PageReference pageRef = new PageReference(ApexPages.currentPage().getHeaders().get('referer'));
    pageRef.setRedirect(True);
    return PageRef;
    }
    public Pagereference returnToAccount(){
        PageReference pageRef = new PageReference('/' + accounts.Id);
    pageRef.setRedirect(True);
    return PageRef;
    }
  
    public void createTaskRecord() {
        NewTask = new Task();
        NewTask.whatId = accounts.Id;
    }
public class OppProductWrapper {
        
    public String OppId {get;set;}
    public String ContractName {get;set;}    
    public String Stage{get;set;}
        public Date CloseDate{get;set;}
        public String ProductName{get;set;}
        public String ProductFamily {get;set;}
        public String ProductSubFamily {get;set;}
        public String ProductCategory {get;set;}
        public String Existing {get;set;}
        public String Competitor {get;set;}
    	public String Contract {get;set;}
    	public String OppName {get;set;}
    	public String NextStep {get;set;}
   		public Date StartDate {get;set;}
        public Date EndDate {get;set;}
        public String ContractStatus {get;set;}
    	public Integer index{get;set;}
        
        public OppProductWrapper(){}
    
    }

}

Thanks in Advance,
Hemdène.
Best Answer chosen by ERP Team
ERP TeamERP Team
Hi Shirisha,

Thank you for your help
I managed to get this into production with 77% code coverage

Best Regards,
Hemdène.

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi,

Greetings!

Unfortunately,we can't provide you the exact test class code to assist you on this request.However,I can provide you the best practices on the test classes with the examples which willl guide you in writing the test classes.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

https://www.forcetalks.com/blog/salesforce-apex-test-class-best-practices/

Please feel free to reach out to the accelerator team who can give you the idea on how you can implement according to your business requirement,if you have premier success plan.

Please find the details on how to request for an accelerator:https://help.salesforce.com/articleView?id=000337601&type=1&mode=1

Otherwise,I would request you to reach out to your account Executive If you wish to upgrade to Premier Support please contact your Account Executive.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
ERP TeamERP Team
Hi Shirisha,

Thank you for your help
I managed to get this into production with 77% code coverage

Best Regards,
Hemdène.
This was selected as the best answer