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
Nitin Palmure 5Nitin Palmure 5 

Write a test class for a wrapper class which adds fields to Visualforce page dynamically

Hello, 

Previously I had written test classes for simple triggers, however I have never written a test class for extension with wrapper class and as complex logic as below. I need some help building a test class for this. 
Following is the extension that I have written and for which I am trying to write a test class.
/*
* Created Date: 04/06/2015
* Created By: Nitin V Palmure
* Description: MeetingAttendeeExt is an extenstion to Standard Controller Key Account Report.
* Visualforce page MeetingAttendee using this extension.
* This is adds field row as per the requirement of user and grabs parent call report ID in the url. This ID is used for pre populating 
* Key Account Report Name and redirection to parent call report after submit or cancel.
*/
public with sharing class MeetingAttendeeExt {
    public List<MeetingAttendeeWrapper> listMeetingAttendeeWrappers{get; set;}
    public static Integer toDelIdent{get; set;}
    public static Integer addCount{get; set;}
    public Integer nextIdent = 0;
    public static Key_Account_Report__c karName = new Key_Account_Report__c();  //variable to prepopulate the Key Account Report lookpup field of Meeting_Attendee__c object
    public String karId; // variable to store the ID of parent call report

    ApexPages.StandardController stdController;
    public MeetingAttendeeExt(ApexPages.StandardController stdController){
        this.stdController=stdController;
        karId = apexpages.currentpage().getParameters().get('Id');
        listMeetingAttendeeWrappers = new List<MeetingAttendeeWrapper>();
         karName= [SELECT Name FROM Key_Account_Report__c WHERE ID =: karId LIMIT 1];
        //for(Integer i = 0; i<5; i++){ //this line is commented so that only one row should be displayed on the page load. if uncommented that 5 rows will get displayed.
            listMeetingAttendeeWrappers.add(new MeetingAttendeeWrapper(nextIdent++,karId));
        //}//end for
       
    }// end constructor
    public void delWrapper(){
        Integer toDelPos = -1;
        for(Integer i = 0; i < listMeetingAttendeeWrappers.size(); i++){
            if(listMeetingAttendeeWrappers[i].ident == toDelIdent){
                toDelPos = i;
            }//End If
        }//End for
        if(-1 != toDelPos){
            listMeetingAttendeeWrappers.remove(toDelPos);
        }//End if
    }//End delWrapper menthod
    public void addRows(){
        for(Integer i = 0; i < addCount; i++){
            listMeetingAttendeeWrappers.add(new MeetingAttendeeWrapper(nextIdent++,karId));
        }//End For
    }//End addRows Method
    
    public PageReference save(){
        List<MeetingAttendee__c> meetAtts = new List<MeetingAttendee__c>();
        for(MeetingAttendeeWrapper wrap : listMeetingAttendeeWrappers){
            meetAtts.add(wrap.meetAtt);
        }//End For
        insert meetAtts;
        return new PageReference('/' + karId);
    }//End Save method
    public PageReference cancel(){
        return new PageReference('/' + karId);
    }//End cancel method
    public class MeetingAttendeeWrapper{
        public MeetingAttendee__c meetAtt{get; private set;}
        public Integer ident{get; private set;}
        public String ID {get; set;}
        
        public MeetingAttendeeWrapper(Integer inIdent, String Id){
            ident = inIdent;
            ID = Id;
            meetAtt = new MeetingAttendee__c(Key_Account_Report__c = Id);
            System.debug('Nitin::; line 51 karName: ' + Id);
        }//End constructor
    }//End wrapper class
}//End Class

Request you to please help me writing a test class for above, I have tried to start, however I am unable to figure out how can I write a test conditions where I will be add almost all the data on runtime.
 
@isTest
private class MeetingAttendeeExtTestClass {
    @isTest(SeeAllData=true)
    public static void testMeetingAttendeeExt(){
        ID karId = 'a0An00000007YGu';
        Key_Account_Report__c kar = [SELECT Name, Id FROM Key_Account_Report__c WHERE Id =: karId LIMIT 1];
        //List<MeetingAttendee__c> meetAtts = new List<MeetingAttendee__c>();
        //Reset all the governer limits.
        Test.startTest();
        //Inform Test Class to set current page as your page where extension is used.
        Test.setCurrentPage(Page.MeetingAttendee);
        
        //Instantiate object of "ApexPages.StandardController" by passing object of standard controller
        ApexPages.StandardController stdController = new ApexPages.StandardController(kar);
        
        //Create object of controller extension by passing object of standardController
        MeetingAttendeeExt ext = new MeetingAttendeeExt(stdController);
        ext.listMeetingAttendeeWrappers.add()
        //Finish Test
        Test.stopTest();
    }  
}

Request you to please help.

Regards, 
Nitin V Palmure
Rajiv Bhatt 16Rajiv Bhatt 16
1. You have hardcoded id in your test class which is not a good practice. It will fail in production as it will not be able to find any such record. You will have to create the Key_Account_Report__c explictly. 
2. Once the Key_Account_Report__c record is created, you will have to set the page parameter using: 
PageReference pageRef = Page.MeetingAttendee;
pageRef.getParameters().put('id', kar.id);
Test.setCurrentPage(pageRef);
 //Instantiate object of "ApexPages.StandardController" by passing object of standard controller
 //Create object of controller extension by passing object of standardController
// invoke controller methods
This should take care of the coverage.
ManojjenaManojjena
Hi Nitin ,

Please check below code and create one test record for Key_Account_Report__c .
@isTest
private class MeetingAttendeeExtTestClass {
    @isTest public static void testMeetingAttendeeExt(){
        Key_Account_Report__c kar =new  Key_Account_Report__c();
		//Add mandatory field here like 
		kar.Name='Dummy Name ';
		Insert kar;
         Test.startTest();
        //Instantiate object of "ApexPages.StandardController" by passing object of standard controller
        ApexPages.StandardController stdController = new ApexPages.StandardController(kar);
        //Create object of controller extension by passing object of standardController
        MeetingAttendeeExt ext = new MeetingAttendeeExt(stdController);
           ext.karId=kar.Id;
		   Integr count;
		   String str;	
		   MeetingAttendeeExt.MeetingAttendeeWrapper meetWrp=new MeetingAttendeeExt.MeetingAttendeeWrapper(count,str);
		   ext.addRows();
		   ext.delWrapper();
		   ext.cancel();
		   ext.save();
        //Finish Test
        Test.stopTest();
    }  
}

Let us know if you still you need any help .
ManojjenaManojjena
Hi Nitin,

Have you checked with the code or still you have issue ?