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
_why the unlucky stiff_why the unlucky stiff 

Question on custom controller test class

Hello Folks, I am a complete newbie to Apex. I have a simple custom controller class for displaying a VF page when a user clicks a custom button. I can't figure out for the life of me how to write a test class for this. Please see the Controller Class and the Test class 'attempt' I made below. Could someone be gracious enough to point me to the right direction?

 

The context from VF to get to this button is : Create a new Case froma record type --> Create a new record for 'Contract Admin Entry' object (available onec the case is created) --> Custom button clicked within this page (custom controller called)

 

Here is the Custom Controller :

 

public class CustomSettingController {
	public string CAEID {Get;set;}
	public Contract_Admin_Entry__c oCAE {get;set;}
	public boolean shouldRender {Get;set;}
	
// Default Constructor
//	 public CustomSettingController(){  }

// Custom Constructor
  public CustomSettingController(ApexPages.StandardController ctl){
	shouldRender = false;
  	CAEID=ApexPages.currentPage().getParameters().get('ID');
  	if(CAEID!=''){
  		 		
  		list<Contract_Admin_Entry__c> lCAE = [SELECT Case_Type__c, Case__c FROM Contract_Admin_Entry__c where ID=:CAEID];
  		if(lCAE.size()>0){
  			oCAE = lCAE.get(0);
			shouldRender = true;
  		} 
  	}
  }
  
// Main Method	
  public list<ContractAdmin__c> getCaseTypeProcesses(){
  		 list<ContractAdmin__c> lCaseTypeProcess = [Select c.Case_Type__c, c.Process__c From ContractAdmin__c c where Case_Type__c=:oCAE.Case_Type__C order by Case_Type__c limit 1000];
  		 if(lCaseTypeProcess==null || lCaseTypeProcess.size()==0){
  		 	ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO,'No Checklist available for this Case Type.'));
  		 }
    		return lCaseTypeProcess;

  }
}

 Here is my attempt to write the Test Class :

 

@isTest
private class CustomSettingController_Test {

    static testMethod void myUnitTest() {
        
        // TO DO: implement unit test
        //Build 8 objects of ContractAdmin__c
        
        list<ContractAdmin__c> lCA = new list<ContractAdmin__c>();
        
        ContractAdmin__c oCA = new ContractAdmin__c();
        ocA.Case_Type__c = 'Canada ADP';
        oCA.Process__c = 'Tier - 1';
        lCA.add(ocA);
        
     	ContractAdmin__c oCA1 = new ContractAdmin__c();
        ocA1.Case_Type__c = 'Canada ADP';
        oCA1.Process__c = 'Are T and C’s attached or T and C confirmation page included';
        lCA.add(ocA1);   
        insert lCA;
        

        //build Case
        
        list<Case> lCase = new list<Case>();
        
        Case oCase = new Case();
        oCase.Tier__c = 'Tier 1';
        oCase.Origin = 'Usage Adjustment';
        oCase.Status = 'Received';
        lCase.add(oCase);

      
        Case oCase1 = new Case();
        oCase1.Tier__c = 'Tier 2';
        oCase1.Origin = 'Contract Admin';
        oCase1.Status = 'In-Progress';
        lCase.add(oCase1);
        
        insert lCase;
        
        //build Contract_Admin_Entry__c
        
        list<Contract_Admin_Entry__c> lCAE = new list<Contract_Admin_Entry__c>();
        
        Contract_Admin_Entry__c oCAE = new Contract_Admin_Entry__c();
     	oCAE.Case_Type__c = 'Canada ADP';
  //   	oCAE.Id = 'CA-00000009';
     	lCAE.add(oCAE);
     	
     	Contract_Admin_Entry__c oCAE1 = new Contract_Admin_Entry__c();
     	oCAE1.Case_Type__c = 'Canada Transfer';
  //   	oCAE1.Id = 'CA-00000010';
     	lCAE.add(oCAE1);
     	
     	insert lCAE;
     	
     	// build Account
     	
     	list<Account> lACC = new list<Account>();
        
        Account oACC = new Account();
     	oACC.Name = 'Ryans Training Center';
     	lACC.add(oACC);
     	
     	Account oACC1 = new Account();
     	oACC1.Name = 'RPM Inc';
     	lACC.add(oACC1);
     	
     	Account oACC2 = new Account();
     	oACC2.Name = 'LDE2';
     	lACC.add(oACC2);
     	
     	insert lACC;
     	
     	
     	// build Contact
     	
     	list<Contact> lCON = new list<Contact>();
        
        Contact oCON = new Contact();
    	oCON.FirstName = 'Anne';
    	oCON.LastName = 'McGraw';
//	   	oCON.Name;
     	lCON.add(oCON);
     	
     	Contact oCON1 = new Contact();
     	oCON1.FirstName = 'Kelly';
     	oCON1.LastName = 'McGill';
//     	oCON1.Name;
     	lCON.add(oCON1);
     	
     	Contact oCON2 = new Contact();
     	oCON2.FirstName = 'Jeff';
     	oCON2.LastName = 'Althaus';
 //    	oCON2.Name;
     	lCON.add(oCON2);
     	
     	insert lCON;
     	
     //Test coverage for the CaseTypeProcess visualforce page
		PageReference pageRef = Page.CaseTypeProcess;
		Test.setCurrentPageReference(pageRef);
		
	// create an instance of the controller
     	Test.startTest();
     	
     	Contract_Admin_Entry__c tCAE = new Contract_Admin_Entry__c();
   		ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(tCAE);
    	CustomSettingController csc = new CustomSettingController(sc);   	
  //   	List nextPage = csc.getCaseTypeProcesses();
     	
     	
     	Test.stopTest();
    }
     
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

Here is your test class

 

@isTest
private class CustomSettingController_Test {

    static testMethod void myUnitTest() {
        
        // TO DO: implement unit test
        //Build 8 objects of ContractAdmin__c
        
        list<ContractAdmin__c> lCA = new list<ContractAdmin__c>();
        
        ContractAdmin__c oCA = new ContractAdmin__c();
        ocA.Case_Type__c = 'Canada ADP';
        oCA.Process__c = 'Tier - 1';
        lCA.add(ocA);
        
     	ContractAdmin__c oCA1 = new ContractAdmin__c();
        ocA1.Case_Type__c = 'Canada ADP';
        oCA1.Process__c = 'Are T and C’s attached or T and C confirmation page included';
        lCA.add(ocA1);   
        insert lCA;
        

        //build Case
        
        list<Case> lCase = new list<Case>();
        
        Case oCase = new Case();
        oCase.Tier__c = 'Tier 1';
        oCase.Origin = 'Usage Adjustment';
        oCase.Status = 'Received';
        lCase.add(oCase);

      
        Case oCase1 = new Case();
        oCase1.Tier__c = 'Tier 2';
        oCase1.Origin = 'Contract Admin';
        oCase1.Status = 'In-Progress';
        lCase.add(oCase1);
        
        insert lCase;
        
        //build Contract_Admin_Entry__c
        
        list<Contract_Admin_Entry__c> lCAE = new list<Contract_Admin_Entry__c>();
        
        Contract_Admin_Entry__c oCAE = new Contract_Admin_Entry__c();
     	oCAE.Case_Type__c = 'Canada ADP';
     	lCAE.add(oCAE);
     	
     	Contract_Admin_Entry__c oCAE1 = new Contract_Admin_Entry__c();
     	oCAE1.Case_Type__c = 'Canada Transfer';
        lCAE.add(oCAE1);
     	
     	insert lCAE;
     	
     	// build Account
     	
     	
     	
       //Test coverage for the CaseTypeProcess visualforce page
        PageReference pageRef = Page.CaseTypeProcess;
	Test.setCurrentPageReference(pageRef);
		
	// create an instance of the controller
     	Test.startTest();
     	
     	Contract_Admin_Entry__c tCAE = new Contract_Admin_Entry__c();
   	
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(tCAE);
        ApexPages.currentPage().getParameters().put('ID', lCAE.get(0).id);
    	CustomSettingController csc = new CustomSettingController(sc);   	
        list<ContractAdmin__c> nextPageCAdmin = csc.getCaseTypeProcesses();
     	
     	
     	Test.stopTest();
    }
}

 I hope it will help you.

All Answers

ColinKenworthy2ColinKenworthy2

Your custom controller's constructor needs an 'ID' parameter and value in order to do anything. So your test method must place it there for the controller to find.

 

 

ApexPages.currentPage().getParameters().put('ID', tCAE.Id);

Obviously that means you have to INSERT the tCAE in order to get an Id for it.

 

I think once you've inserted the tCAE and set up the parameter in ApexPages the controller csc should return you a List if you call getCaseTypeProcesses on it.

Shashikant SharmaShashikant Sharma

Here is your test class

 

@isTest
private class CustomSettingController_Test {

    static testMethod void myUnitTest() {
        
        // TO DO: implement unit test
        //Build 8 objects of ContractAdmin__c
        
        list<ContractAdmin__c> lCA = new list<ContractAdmin__c>();
        
        ContractAdmin__c oCA = new ContractAdmin__c();
        ocA.Case_Type__c = 'Canada ADP';
        oCA.Process__c = 'Tier - 1';
        lCA.add(ocA);
        
     	ContractAdmin__c oCA1 = new ContractAdmin__c();
        ocA1.Case_Type__c = 'Canada ADP';
        oCA1.Process__c = 'Are T and C’s attached or T and C confirmation page included';
        lCA.add(ocA1);   
        insert lCA;
        

        //build Case
        
        list<Case> lCase = new list<Case>();
        
        Case oCase = new Case();
        oCase.Tier__c = 'Tier 1';
        oCase.Origin = 'Usage Adjustment';
        oCase.Status = 'Received';
        lCase.add(oCase);

      
        Case oCase1 = new Case();
        oCase1.Tier__c = 'Tier 2';
        oCase1.Origin = 'Contract Admin';
        oCase1.Status = 'In-Progress';
        lCase.add(oCase1);
        
        insert lCase;
        
        //build Contract_Admin_Entry__c
        
        list<Contract_Admin_Entry__c> lCAE = new list<Contract_Admin_Entry__c>();
        
        Contract_Admin_Entry__c oCAE = new Contract_Admin_Entry__c();
     	oCAE.Case_Type__c = 'Canada ADP';
     	lCAE.add(oCAE);
     	
     	Contract_Admin_Entry__c oCAE1 = new Contract_Admin_Entry__c();
     	oCAE1.Case_Type__c = 'Canada Transfer';
        lCAE.add(oCAE1);
     	
     	insert lCAE;
     	
     	// build Account
     	
     	
     	
       //Test coverage for the CaseTypeProcess visualforce page
        PageReference pageRef = Page.CaseTypeProcess;
	Test.setCurrentPageReference(pageRef);
		
	// create an instance of the controller
     	Test.startTest();
     	
     	Contract_Admin_Entry__c tCAE = new Contract_Admin_Entry__c();
   	
        ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(tCAE);
        ApexPages.currentPage().getParameters().put('ID', lCAE.get(0).id);
    	CustomSettingController csc = new CustomSettingController(sc);   	
        list<ContractAdmin__c> nextPageCAdmin = csc.getCaseTypeProcesses();
     	
     	
     	Test.stopTest();
    }
}

 I hope it will help you.

This was selected as the best answer
ColinKenworthy2ColinKenworthy2

Actually there is no need for the tCAE variable at all !!

_why the unlucky stiff_why the unlucky stiff

Thank You.