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
Liz Gibbons 16Liz Gibbons 16 

Test class for controller with PageReference

Hello, I am working on writing a test class for a custom controller that creates an Account list, and then based on the selected Account's ID, redirects to a new page. I'm new to this so any help would be appreciated. 
Controller:
public without sharing class SDPController {
    public List<Account> PAISchools = new List<Account>();
    public SDPController(){   
    }

    public List<SelectOption> PAIList{
        get{
            PAISchools = [Select Account.Name, Account.Id From Account Where Account.ParentId = '0011700000BZkKR'];
            
            PAIList = new List<SelectOption>();
            
            for(Account temp : PAISchools){
                PAIList.add(new SelectOption(temp.id, temp.Name));
            }
            return PAIList;
        }
        set;    
    }
    
    public String idString{get;set;}
    
    
	public PageReference saveform(){
  		update PAISchools ; // helps in saving the record
  			PageReference pageRef = new PageReference('https://dev2-dor.cs22.force.com/eportfolio/LPRegistration?code='+idString+'');
            	pageRef.setRedirect(true);
            	return pageRef;
	}
    
    public PageReference signin(){
        PageReference signIn = new PageReference('https://dev2-dor.cs22.force.com/eportfolio/motivislp__LPSignIn');
        signIn.setRedirect(true);
        return signIn;
    }
  
        
}

Test class:
@isTest
public class TestSDPController {
    
    public static testMethod void testSDP() {  
       
       Account acc = new Account(name= 'Testing', ParentId= '0011700000BZkKR');
        insert acc;
       
       Test.startTest();
       PageReference testPage = Page.SchoolSelect2;
        Test.setCurrentPage(testPage);
        testPage.getUrl();
        
        SDPController sdp = new SDPController();
        	sdp.saveform();
        Test.stopTest();
        
              
     }

}

Am I at least on the right path?
Best Answer chosen by Liz Gibbons 16
Marek Kosar_Marek Kosar_
Hi Liz,

code below, with comments inside:
public without sharing class SDPController {
    public List<Account> PAISchools = new List<Account>();
	
    public SDPController(){ 
		//1 - get ID from url, I suppose this controller is used for some VF page and you need insert id into url as parameter (like: /apex/vfpagename?id=someId)
		idString = ApexPages.currentPage().getParameters().get('id')
    }

    public List<SelectOption> PAIList{
        get{
			//2 - then you need to use Id from url to query proper account
            PAISchools = [Select Account.Name, Account.Id From Account Where Account.ParentId =: idFromUrl];
            
            PAIList = new List<SelectOption>();
            
            for(Account temp : PAISchools){
                PAIList.add(new SelectOption(temp.id, temp.Name));
            }
            return PAIList;
        }
        set;    
    }
    
    public String idString{get;set;}
    
	public PageReference saveform(){
  		update PAISchools ; // helps in saving the record
		PageReference pageRef = new PageReference('https://dev2-dor.cs22.force.com/eportfolio/LPRegistration?code='+idString);
		pageRef.setRedirect(true);
		return pageRef;
	}
    
    public PageReference signin(){
        PageReference signIn = new PageReference('https://dev2-dor.cs22.force.com/eportfolio/motivislp__LPSignIn');
        signIn.setRedirect(true);
        return signIn;
    }

}
 
@isTest
public class TestSDPController {
    
    public static testMethod void testSDP() {  
       
	   //3 do not hardcode ID, never! create test data every time
	   Account parentAcc = new Account(name= 'Testing parent');
       insert parentAcc;
	   
       Account childAcc = new Account(name= 'Testing child', ParentId= parentAcc.Id);
       insert childAcc;
       
       Test.startTest();
       PageReference testPage = Page.SchoolSelect2;
        Test.setCurrentPage(testPage);
		
		//4 - not why is this here, becouse there is no `Url` in controller
        testPage.getUrl();
		
		//5 - you use id of created child account, to test your VF page
		ApexPages.currentPage().getParameters().put('id', childAcc.Id);
        
        SDPController sdp = new SDPController();
			//6 - do not forget to assert results you get, to make sure your test is ok !! ;)
			List<SelectOption> selectOpt = sdp.PAIList();
        	sdp.saveform();
			spd.signin();
        Test.stopTest();   
     }
}
Hope it will help a little bit.


Marek

All Answers

Marek Kosar_Marek Kosar_
Hi Liz,

code below, with comments inside:
public without sharing class SDPController {
    public List<Account> PAISchools = new List<Account>();
	
    public SDPController(){ 
		//1 - get ID from url, I suppose this controller is used for some VF page and you need insert id into url as parameter (like: /apex/vfpagename?id=someId)
		idString = ApexPages.currentPage().getParameters().get('id')
    }

    public List<SelectOption> PAIList{
        get{
			//2 - then you need to use Id from url to query proper account
            PAISchools = [Select Account.Name, Account.Id From Account Where Account.ParentId =: idFromUrl];
            
            PAIList = new List<SelectOption>();
            
            for(Account temp : PAISchools){
                PAIList.add(new SelectOption(temp.id, temp.Name));
            }
            return PAIList;
        }
        set;    
    }
    
    public String idString{get;set;}
    
	public PageReference saveform(){
  		update PAISchools ; // helps in saving the record
		PageReference pageRef = new PageReference('https://dev2-dor.cs22.force.com/eportfolio/LPRegistration?code='+idString);
		pageRef.setRedirect(true);
		return pageRef;
	}
    
    public PageReference signin(){
        PageReference signIn = new PageReference('https://dev2-dor.cs22.force.com/eportfolio/motivislp__LPSignIn');
        signIn.setRedirect(true);
        return signIn;
    }

}
 
@isTest
public class TestSDPController {
    
    public static testMethod void testSDP() {  
       
	   //3 do not hardcode ID, never! create test data every time
	   Account parentAcc = new Account(name= 'Testing parent');
       insert parentAcc;
	   
       Account childAcc = new Account(name= 'Testing child', ParentId= parentAcc.Id);
       insert childAcc;
       
       Test.startTest();
       PageReference testPage = Page.SchoolSelect2;
        Test.setCurrentPage(testPage);
		
		//4 - not why is this here, becouse there is no `Url` in controller
        testPage.getUrl();
		
		//5 - you use id of created child account, to test your VF page
		ApexPages.currentPage().getParameters().put('id', childAcc.Id);
        
        SDPController sdp = new SDPController();
			//6 - do not forget to assert results you get, to make sure your test is ok !! ;)
			List<SelectOption> selectOpt = sdp.PAIList();
        	sdp.saveform();
			spd.signin();
        Test.stopTest();   
     }
}
Hope it will help a little bit.


Marek
This was selected as the best answer
Liz Gibbons 16Liz Gibbons 16
Thanks Marek! It's working now. And thank you for the pointers on the controller as well. I'm pretty new at this.