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
KitagawaSan85KitagawaSan85 

VF Page Test Class

I am a bit new to writing visualforce classes so any help is greatly appreciated... 

 

I have written a visualforce page and controller extention that are working in my sandbox, but now I have to create the test class. 

 

Controller Extension which pretty much just creates some values for me:  

public class OpportunityExt {

    private final Opportunity opp;
    
    public OpportunityExt(ApexPages.StandardController stdController) {
        this.opp = (opportunity)stdController.getRecord();
    }
    

    public integer getOpenTasks() {
        return [SELECT count() FROM task WHERE whatid = :opp.id and IsClosed=false];
    } 

   
    public decimal getFunnel() {
        AggregateResult ar = [SELECT Sum(Lic_Revenue__c)s FROM opportunity WHERE ownerid = :opp.ownerid and Close_FQ__c = :opp.Close_FQ__c];
        decimal d1, dec;  
        if (ar.get('s') != null){
            dec = opp.Lic_Revenue__c / (decimal)ar.get('s') * 100;
            }
            else {
            dec = 0.00;}
            
        return d1.round();
    }
    
    public decimal getProposalScore() {
        AggregateResult ar2 = [SELECT AVG(total_score__c)a FROM Proposal__c WHERE Opportunity__c = :opp.id];
        decimal p1 = (decimal)ar2.get('a');
        return p1;
    }
     
     
    public string getShortID() {
        string longid = opp.id;
        return longid.substring(0,15);
        }
}

 

 
Visualforce Page which displays them: 

 

<apex:page showHeader="false"  standardStylesheets="true" sidebar="true" standardController="Opportunity" extensions="OpportunityExt">

<table align='center'  width="90%">
    <tr>

    <td align="center" style='border-right:1px solid black;'>
        <span  style="font-size: 20pt" align='center'>
            <apex:outputText value="{!CASE(opportunity.StageName,"Targeted Accounts","Target","Marketing Qualified", "Qualifed", "Auditor Communicating UCA Results","UCA","Channel Communicating UCA Results","UCA","Sales Rep Communicating UCA Results","UCA","1 - Determine interest in QAD","1","2 - Go/No Go Checkpoint","2","3 - Identify Sponsor/Create Vision Stmt","3","4 - Detailed Discovery","4","5 - Solution Confirmation","5","6 - Create Proposal","6","7 - On Hold","7","8 - Notification of Verbal Selection","8","Win","Win!","Loss","Loss","Invoiced Win","Win!","Qualified Out","QO","Lead Qualified Out","QO","Error")}" id="stage" />
        </span>
        <br/>
        Stage
    </td>
  
    <td align='center' style='border-right:1px solid black;'>
        <span  style="font-size: 20pt" align='center'>
            <apex:outputField value="{!opportunity.Lic_Revenue__c}"/>
        </span>
        <br/>
        <apex:outputText >License</apex:outputText>
    </td>
    
    <td align='center' style='border-right:1px solid black;'>
        <span  style="font-size: 20pt">
            <apex:outputField value="{!opportunity.Age_Custom__c}"/>
        </span>
        <br/>
        <apex:outputText >Days Open</apex:outputText>
    </td>
    
    <td align='center' style='border-right:1px solid black;'>

 

For the test class I have this so far, but I'm not sure I am moving in the right direction: 

public class testMyPage {
static testMethod void OpportunityExt()
{
//Test converage for the myPage visualforce page
PageReference pageRef = Page.OpportunityQuickView;
Test.setCurrentPageReference(pageRef);

//Create Account
Account ac = new Account (name='XYZ Organization');
insert ac;


//Create Opportunity
Opportunity op = new Opportunity (name='test opportunity');
op.Account = ac;
op.Type = 'New Users';
op.Lic_Revenue__c = 5; 
op.StageName = 'Win'; 
op.Push_Count_FQ__c = 2; 
op.CloseDate = system.today();
insert op;


ApexPages.StandardController sc = new ApexPages.standardController(op);
// create an instance of the controller
OpportunityExt myPageOpp = new OpportunityExt(sc);

//try calling methods/properties of the controller in all possible scenarios
// to get the best coverage.
integer OpenTasks = myPageOpp.GetOpenTasks();
decimal Funnel = myPageOpp.GetFunnel();
decimal ProposalScore = myPageOpp.GetProposalScore();
string ShortID = myPageOpp.GetShortID();
}
} 

Currently, when I run the test I get a 'System.QueryException: List has no rows for assignment to SObject' error and 18% coverage in my controller. 

 

Is there a better way I can test this? I have never written a visualforce controller test class before... 

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
KitagawaSan85KitagawaSan85

Was able to figure it out... 

 

testclass

public class testMyPage {
static testMethod void OpportunityExt()
{
//Test converage for the myPage visualforce page
PageReference pageRef = Page.OpportunityQuickView;
Test.setCurrentPageReference(pageRef);

//Create Account
Account ac = new Account (name='XYZ Organization');
insert ac;


//Create Opportunity
Opportunity op = new Opportunity (name='test opportunity');
op.Account = ac;
op.Type = 'New Users';
op.Lic_Revenue__c = 5; 
op.StageName = 'Win'; 
op.Push_Count_FQ__c = 2; 
op.CloseDate = system.today();
insert op;

//Create Opportunity
Opportunity op2 = new Opportunity (name='test opportunity');
op2.Account = ac;
op2.Type = 'New Users';
op2.Lic_Revenue__c = null; 
op2.StageName = 'Win'; 
op2.Push_Count_FQ__c = 2; 
op2.CloseDate = system.today();
insert op2;

ApexPages.StandardController sc = new ApexPages.standardController(op);
// create an instance of the controller
OpportunityExt myPageOpp = new OpportunityExt(sc);

//try calling methods/properties of the controller in all possible scenarios
// to get the best coverage.
integer OpenTasks = myPageOpp.GetOpenTasks();
decimal Funnel = myPageOpp.GetFunnel();
decimal ProposalScore = myPageOpp.GetProposalScore();
string ShortID = myPageOpp.GetShortID();


ApexPages.StandardController sc2 = new ApexPages.standardController(op2);
// create an instance of the controller
OpportunityExt myPageOpp2 = new OpportunityExt(sc2);

//try calling methods/properties of the controller in all possible scenarios
// to get the best coverage.
integer OpenTasks2 = myPageOpp2.GetOpenTasks();
decimal Funnel2 = myPageOpp2.GetFunnel();
decimal ProposalScore2 = myPageOpp2.GetProposalScore();
string ShortID2 = myPageOpp2.GetShortID();
}
}

 

controller ext

 

public class OpportunityExt {

    private final Opportunity opp;
    
    public OpportunityExt(ApexPages.StandardController stdController) {
        this.opp = (opportunity)stdController.getRecord();
    }
    

    public integer getOpenTasks() {
        return [SELECT count() FROM task WHERE whatid = :opp.id and IsClosed=false];
    } 

   
    public decimal getFunnel() {
        AggregateResult ar = [SELECT Sum(Lic_Revenue__c)s FROM opportunity WHERE ownerid = :opp.ownerid and Close_FQ__c = :opp.Close_FQ__c ];
        decimal lic = opp.lic_revenue__c;
        if (lic == null || ar.get('s') == null) {return 0;} else {
        
        decimal d1 = opp.Lic_Revenue__c / (decimal)ar.get('s');
        return d1.setScale(1)*100;}
    }
    
    public decimal getProposalScore() {
        AggregateResult ar2 = [SELECT AVG(total_score__c)a FROM Proposal__c WHERE Opportunity__c = :opp.id];
        decimal p1 = (decimal)ar2.get('a');
        return p1;
    }
     
     
    public string getShortID() {
        string longid = opp.id;
        return longid.substring(0,15);
        }
}