+ Start a Discussion
Michael Clarke 36Michael Clarke 36 

How do I test vf page and controller extension

Hi, I have built my first vf page and controller extension and it works fine in Sandbox.
How do I create a Test class for this?

Visualforce Page:
<apex:page standardController="Case" extensions="Case_ListOppSplits_Controller" lightningStylesheets="true">
    <apex:pageBlock>
    	<apex:pageBlockTable value="{!Opportunity_Splits}" var="oppSplit">
            <apex:column value="{!oppSplit.Name}"/>
            <apex:column value="{!oppSplit.Split_Loan_Amount__c}"/>
            <apex:column value="{!oppSplit.Rate_Type__c}"/>
            <apex:column value="{!oppSplit.Repayment_Type__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

Controller Extension Class:
public class Case_ListOppSplits_Controller {

    public Case myCase;
    
    public Case_ListOppSplits_Controller(ApexPages.StandardController stdController){
        this.myCase = (Case)stdController.getRecord();

    }

//initialise setController  and return  a list of records
    public list<Opportunity_Split__c> getOpportunity_Splits(){
        Case currentCase = [SELECT Id, Subject, Opportunity__c FROM Case WHERE Id =: ApexPages.currentPage().getParameters().get('id')];
        List<Opportunity_Split__c> OppSplit = [SELECT Id, Name, Opportunity__c, Loan_Purpose__c, Loan_Type__c, Loan_Usage__c, Rate_Type__c, Repayment_Type__c, Split_Loan_Amount__c
                     FROM Opportunity_Split__c
                     WHERE Opportunity__c =: currentCase.Opportunity__c];
        return OppSplit;
    }
    
}

​​​​​​​
ANUTEJANUTEJ (Salesforce Developers) 
Hi Michael,

Can you try checking the below link as it has a similar use case test class that could help:

>> https://developer.salesforce.com/forums/?id=9060G000000Bj8MQAS

Let me know if this helps and in case if this comes handy can you please choose this as best answer so that it can be used by others in the future.

Regards,
Anutej
Michael Clarke 16Michael Clarke 16
Hi Anutej,
Thanks for the link.
I have attempted to adjust to my requirements.
I have this test code with one error: variable does not ext - OppSplit
@isTest
public class Case_ListOppSplits_Controller_Test {
//To be completed
	static testMethod void testMethod1() 
	{
		Case testCase = new Case();
		testCase.Subject='Test Case';
		testCase.Opportunity__c='0067F00000N8vSVQAZ';
		testCase.RecordTypeId='0126D000000qSBcQAM'; //UA
		testCase.Status='Assigned';
		insert testCase;

		Test.StartTest(); 
            ApexPages.StandardController sc = new ApexPages.StandardController(testCase);
			Case_ListOppSplits_Controller obj = new Case_ListOppSplits_Controller(sc);
			List<Case_ListOppSplits_Controller> listOppSplits = obj.OppSplit;
		Test.StopTest();
	}
}
I don't fully understand what is going on here but based on the code in the link, I can't see the problem with OppSplit - the variable returned by the controller extension.