0
votes

I am new to coding and am using Salesforce. I need help understanding how to create a test class for a controller extension. I am clutching at straws when building the Test Class and now have 66% code coverage. Your help to adjust the below code will be much appreciated and help greatly in my understanding. The Visualforce page is very simple:

<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.Loan_Usage__c}"/>
        <apex:column value="{!oppSplit.Loan_Purpose__c}"/>
        <apex:column value="{!oppSplit.Rate_Type__c}"/>
        <apex:column value="{!oppSplit.Repayment_Type__c}"/>
    </apex:pageBlockTable>
</apex:pageBlock>
</apex:page>`

The controller extension is:

public class Case_ListOppSplits_Controller {
    public Case myCase;

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

    }
    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> OppSplits = [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 OppSplits;
    }   
}

Test Class:

@isTest
public class Case_ListOppSplits_Controller_Test {
    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(); 
    
        PageReference pageRef = new PageReference('https://omniwealth--dwr.my.salesforce.com/apex/Case_OpportunitySplits?id='+testCase.Id); 
    
        ApexPages.StandardController sc = new ApexPages.StandardController(testCase);
        Case_ListOppSplits_Controller extn = new Case_ListOppSplits_Controller(sc);
        List<Opportunity_Split__c> listOppSplits = extn.getOpportunity_Splits(); 
    
        Test.StopTest();
    }
}
1
You must create Opportunity__c first and insert case after that. Your test class will not show you real data. (use @seeAllData = true but it's not good solution)Nguyễn Thắng
Thx for the tip - I had forgotten about this best practice rule. I will remember now.Michael

1 Answers

1
votes

It looks like you are trying to utilize existing data (which you shouldn't do) without adding @seeAlldata=true. What you should do is create the opportunities and splits in your test code before you call your controller methods.

On a side note, you have the myCase record in your controller, so your query in getOpportunitySplits can utilize it instead of the page parameter. (Where Id =: myCase.Id).

Once you create the opportunity and splits or set seealldata, you should increase the coverage. If you decided to use seeAllData, know that your test will fail when attempting to deploy to production since the opportunity id values probably won't match.