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
Uttpal chandraUttpal chandra 

How to write a test class for this apex classs

Hi Anyone help me how to write test class for this ?
 
public class theController {

	public List<Wrapper> contactList {get; set;}
    public static String mass {get;set;}
    public static boolean saveSuccessful {get; set;}
 	public static string message {get; set;}
    ID Id = ApexPages.currentPage().getParameters().get('id');
    public Integer offSetSize = 0;
    public Integer QueryLimit = 10;
	public Integer countTotalRecords{get;set;}



public theController(ApexPages.StandardController controller){


				countTotalRecords = [SELECT count() FROM Activity_Template__c WHERE For_Which_Object__c = 'Account'];
}

     
public List<Wrapper> getContacts() {
    
   if(contactList == null) {
        contactList = new List<Wrapper>();
        for(Activity_Template__c c: [Select Name,Template_Tool_Tip__c,Template_Type__c FROM
                                        Activity_Template__c WHERE For_Which_Object__c = 'Account' LIMIT :QueryLimit OFFSET :offSetSize ])
     		       
        {
            contactList.add(new Wrapper(c));
           	
        }
    }
    
    return contactList;
}
         
public boolean getprv(){
        if(offSetSize > 0)
        	return false;
    	else
        	return true;
    }
    
public boolean getnxt(){
        if(offSetSize + queryLimit < CountTotalRecords )
        	return false;
    	else
        	return true;
    }
    
public PageReference nextbtn(){
    
        contactList=null; 	
     	offSetSize += queryLimit ;
    	return null;
    }
 
public PageReference prvbtn(){
     
     	contactList=null;
        offSetSize -= queryLimit ;
    	return null;
    }
    
public Integer getPageNumber() {
        return offSetSize/QueryLimit + 1;
 }

public Integer getTotalPages() {
        if (math.mod(countTotalRecords, QueryLimit) > 0) {
            return countTotalRecords/QueryLimit + 1;
        } else {
            return (countTotalRecords/QueryLimit);
        }
    }    
     
    
public PageReference processSelected() {
  
    List<Activity_Template__c> selectedContacts = new List<Activity_Template__c>();

			   
    for(Wrapper cCon: getContacts()) {
        if(cCon.selected == true) {
            selectedContacts.add(cCon.con); 
        }
           
    }
   

    for(Activity_Template__c con: selectedContacts) {
       system.debug(con);
        
    }
    

//	addParentAccount(id,selectedContacts);

    contactList=null; 	
    return null;    
}
     

     
public class Wrapper {
    public Activity_Template__c con {get; set;}
    public Boolean selected {get; set;}

     public Wrapper(Activity_Template__c c) {
        con = c;
        selected = false;
    	}
}
}

Thanks in advance
Deepali KulshresthaDeepali Kulshrestha
 Hi Uttpal,

 I went through the query of yours, Please go through these links :

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
https://developer.salesforce.com/forums/?id=9060G000000BgOWQA0
https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 
Raj VakatiRaj Vakati
Use this code
 
@isTest 
public class ExtensionTestClass 
{
	static testMethod void testMethod1() 
	{
		Activity_Template__c  obj = new Activity_Template__c ();
		obj.For_Which_Object__c = 'Account'
		// Add all required field here
		insert obj;
	
		
		Activity_Template__c  obj1= new Activity_Template__c ();
		obj1.For_Which_Object__c = 'Account'
		// Add all required field here
		insert obj1;
	
		Activity_Template__c  obj2 = new Activity_Template__c ();
		obj2.For_Which_Object__c = 'Account'
		// Add all required field here
		insert obj2;
	

		Test.StartTest(); 
		
			ApexPages.currentPage().getParameters().put('Id', String.valueOf(exp.Id));
			ApexPages.StandardController sc = new ApexPages.StandardController(eli);
			theController  obj = new theController (sc);
			obj.getContacts(); 
			obj.getprv(); 
			obj.getnxt(); 
			obj.nextbtn();
			obj.prvbtn();
			obj.getPageNumber();
			obj.getTotalPages();
			obj.processSelected();




		Test.StopTest();
	}
}

 
Uttpal chandraUttpal chandra
Hi Raj,

Thanks for reply right now giving two errors:


Variable does not exist: exp
Variable does not exist: eli

 
Raj VakatiRaj Vakati
Can you check is this error are from this class ?? we dnt have any variables exp andeli
Uttpal chandraUttpal chandra
ApexPages.currentPage().getParameters().put('Id', String.valueOf(exp.Id));
ApexPages.StandardController sc = new ApexPages.StandardController(eli);


See in above line:

Test class line no 25 and 26.

 
Uttpal chandraUttpal chandra
Hi Raj,
Your code works awesome but can you help me in the negative testing part.
 
Raj VakatiRaj Vakati
Please refer this link and make sure your test class is failing and handler the trey and catch ... 

Please close this thread 

https://trailhead.salesforce.com/en/content/learn/modules/unit-testing-on-the-lightning-platform/negative-tests