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
sudh shuklasudh shukla 

some one plz help me to write test class for the following wrapper class.

public with sharing class controller_for_page {
    
    public List<accountWrapper> display_list {get; set;} //list for all Account records and a row counter
    public List<String> current_list = new List<String>(); //list for holding many record Ids
    public List<String> next_list = new List<String>(); //list for holding record Ids that are after the current records
    public List<String> previous_list = new List<String>(); //list for holding record Ids that are before the current records
    Integer list_size = 50; //number of records to display on the page
    
    //initiates the controller and displays some initial data when the page loads
    public controller_for_page() {
        Integer record_counter = 0; //counter
        for (Account a : [SELECT Id FROM Account ORDER BY Name LIMIT 10000]) { //for a bunch of accounts
            if (record_counter < list_size) { //if we have not yet reached our maximum list size
                current_list.add(a.Id); //add the Id of the record to our current list
            } else { //otherwise, we reached our list size maximum
                next_list.add(a.Id); //add the Id to our next_list
            }
            record_counter++;
        }
    }
    
    public List<accountWrapper> getRecordsToDisplay() {
        Set<String> record_ids = new Set<String>(); //set for holding distinct Ids
        Boolean records_added = record_ids.addAll(current_list); //add all the records from our current_list list
        display_list = new List<accountWrapper>(); //set the display_list object to a new accountWrapper List
        Integer counter = 1; //row counter variable
        for (Account a : [SELECT AccountNumber, Id, Name, OwnerId, Phone, Site, Type FROM Account WHERE Id in : record_ids ORDER BY Name]) { //query for the details of the records you want to display
            display_list.add(new accountWrapper(a, counter)); //add the account and counter to our list
            counter++; //increment the counter
        }
        return display_list; //return the list of full records plus their row counter
    }
    
    public class accountWrapper {
        public Account act {get; set;} //Account object
        public Integer numberOfRow {get; set;} //row counter variable
        
        public accountWrapper(Account a, Integer rowCounter) {
            this.act = a; //assign account
            this.numberOfRow = rowCounter; //assign row counter
        }
    }
    
    public Integer getCurrentSize() {
        return current_list.size(); //number of record in current_list
    }
    
    public Integer getPrevSize() {
        return previous_list.size(); //number of record in previous_list
    }
    
    public Integer getNextSize() {
        return next_list.size(); //number of record in next_list
    }

}
Best Answer chosen by sudh shukla
ManojjenaManojjena
Hi Sudh,

Try below code and modify your class code as I have suggested it will give you more then 90 % code coverege .
One more thing you need to add accoun mandatory fields .
@isTest
public class Testcontroller_for_page{
    private static  testMethod void unitTest(){
        Test.startTest();
			Account acc=new Account();
			acc.Name='Manoj';
			//Add mandatory fields here 
			Insert acc;
			acc=[SELECT Name FROM Account WHERE id=:acc.Id];
			System.assertEquals(acc.Name,'Manoj');
			Integer count=6;
			controller_for_page  cp=new controller_for_page();
			cp.list_size=50;
			controller_for_page.accountWrapper cwrp=new controller_for_page.accountWrapper(acc,count);
				cp.getRecordsToDisplay();
				cp.getCurrentSize();
				cp.getPrevSize();
				cp.getNextSize();
		Test.stopTest();
    
    }

}
 
 Modify your class code line number seven(7) like below 
 @TestVisible Integer list_size = 50; //number of records to display on the page


LEt me know if it helps !!
Thanks
Manoj

All Answers

ManojjenaManojjena
Hi Sudh ,

Check the below link and start writing test class then ask your doubts which will definitly help you write better test class .

http://manojjena20.blogspot.in/2015/06/tips-and-tricks-for-test-class.html 
Please start then ask questions which will definitly help !!
Thanks
Manoj
sudh shuklasudh shukla
hello manoj ;
i already used that syntax for wrapper class but it shows the error constructor not defined.
ManojjenaManojjena
Hi Sudh,
You need to create instance of wrapper class by passing account instance and integer instance like belwo .
 
Account acc=new Account();
    Integer count=6;
    controller_for_page.accountWrapper cwrp=new controller_for_page.accountWrapper(acc,count);

Still you are facing issue please let me know .
Thanks
Manoj
ManojjenaManojjena
Hi Sudh,

Try below code and modify your class code as I have suggested it will give you more then 90 % code coverege .
One more thing you need to add accoun mandatory fields .
@isTest
public class Testcontroller_for_page{
    private static  testMethod void unitTest(){
        Test.startTest();
			Account acc=new Account();
			acc.Name='Manoj';
			//Add mandatory fields here 
			Insert acc;
			acc=[SELECT Name FROM Account WHERE id=:acc.Id];
			System.assertEquals(acc.Name,'Manoj');
			Integer count=6;
			controller_for_page  cp=new controller_for_page();
			cp.list_size=50;
			controller_for_page.accountWrapper cwrp=new controller_for_page.accountWrapper(acc,count);
				cp.getRecordsToDisplay();
				cp.getCurrentSize();
				cp.getPrevSize();
				cp.getNextSize();
		Test.stopTest();
    
    }

}
 
 Modify your class code line number seven(7) like below 
 @TestVisible Integer list_size = 50; //number of records to display on the page


LEt me know if it helps !!
Thanks
Manoj
This was selected as the best answer