我是编码新手,正在使用 Salesforce。我需要帮助了解如何为控制器扩展创建测试类。在构建测试类时,我正抓着稻草,现在代码覆盖率为 66%。您对调整以下代码的帮助将不胜感激,并对我的理解有很大帮助。Visualforce 页面非常简单:
<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>`
控制器扩展是:
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;
}
}
测试类:
@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();
}
}