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
AshleyHAshleyH 

Assistance Needed with Test for Apex Class called by Page with URL parameters

I have successfully created a page controlled by an Apex Class in my Sandbox and everything works very nicely. Basically it is a report of sorts as the page simply is listing a number of enteries on my opportunity table and a custom payment table. There are not any inputs or buttons the page calls all the information it needs together via URL parameters. My struggle is that I cannot get my test to go through and select any records, therefore my tests are not covering enough of the code and I can't deploy my class and page. Could someone please help me?

 

Here is my class

public class ConfServicesPaymentListing{
    public String prop;
    public String whom;
    public ConfServicesPaymentListing() {
        this.prop = ApexPages.currentPage().getParameters().get('property');
        this.whom = ApexPages.currentPage().getParameters().get('who'); }
    public void setProp(String prop) { this.prop = prop; }
    public void setWhom(String whom) { this.whom = whom; }
    
    public Date tday   = Date.Today();
    public Integer tmonth   = Date.Today().month();
    public Integer tyear    = Date.Today().year();
    public Integer daysThis = Date.daysInMonth(tyear, tmonth);
    public Date LastMonth = Date.Today()-daysThis;
    public Date NextMonth = Date.Today()+daysThis;
    public Date thisStart = Date.Today().toStartOfMonth();
    public Date lastStart = LastMonth.toStartOfMonth();
    public Date thisEnd   = NextMonth.toStartOfMonth();
    //Commissionable Bill Pay
    public Payments__c[] getPaymentBillThisComissionableList() {
        Payments__c[] pbtcl = [SELECT 
                                  Booking_Name_Organization__c, Commissionable__c, Date_Recieved__c, Event_Dates__c, Notes__c, Payment__c, Type__c
                              FROM 
                                  Payments__c
                              WHERE
                                  Password__c=:prop AND Commissionable__c='Commissionable' AND Type__c='Bill Pay' AND Date_Recieved__c>=:thisStart AND Date_Recieved__c<:thisEnd                                  
                              ORDER BY Date_Recieved__c
                              ];
        return pbtcl;
    }
    Payments__c pbtcl2;
    public Payments__c getPbtcl2(){
        pbtcl2 = new Payments__c(Payment__c = 0);
        for(Payments__c pbtc : getPaymentBillThisComissionableList()){
        pbtcl2.Payment__c += pbtc.Payment__c; }
        return pbtcl2;  
    }
    //Commissionable Bill Pay Last Month
    public Payments__c[] getPaymentBillLastComissionableList() {
        Payments__c[] pbtcl = [SELECT 
                                  Booking_Name_Organization__c, Commissionable__c, Date_Recieved__c, Event_Dates__c, Notes__c, Payment__c, Type__c
                              FROM 
                                  Payments__c
                              WHERE
                                  Password__c=:prop AND Commissionable__c='Commissionable' AND Type__c='Bill Pay' AND Date_Recieved__c>=:lastStart AND Date_Recieved__c<:thisStart                                  
                              ORDER BY Date_Recieved__c
                              ];
        return pbtcl;
    }
    Payments__c pblcl2;
    public Payments__c getPblcl2(){
        pblcl2 = new Payments__c(Payment__c = 0);
        for(Payments__c pblc : getPaymentBillLastComissionableList()){
        pblcl2.Payment__c += pblc.Payment__c; }
        return pblcl2;  
    }
    //Commissionable Deposit Pay This Month
    public Payments__c[] getPaymentDepositThisComissionableList() {
        Payments__c[] pdtcl = [SELECT 
                                  Booking_Name_Organization__c, Commissionable__c, Date_Recieved__c, Event_Dates__c, Notes__c, Payment__c, Type__c
                              FROM 
                                  Payments__c
                              WHERE
                                  Password__c=:prop AND Commissionable__c='Commissionable' AND Type__c='Deposit' AND Date_Recieved__c>=:thisStart AND Date_Recieved__c<:thisEnd                                  
                              ORDER BY Date_Recieved__c
                              ];
        return pdtcl;
    }
    Payments__c pdtcl2;
    public Payments__c getPdtcl2(){
        pdtcl2 = new Payments__c(Payment__c = 0);
        for(Payments__c pdtc : getPaymentDepositThisComissionableList()){
        pdtcl2.Payment__c += pdtc.Payment__c; }
        return pdtcl2;  
    }
    //Commissionable Deposits Last Month
    public Payments__c[] getPaymentDepositLastComissionableList() {
        Payments__c[] pdlcl = [SELECT 
                                  Booking_Name_Organization__c, Commissionable__c, Date_Recieved__c, Event_Dates__c, Notes__c, Payment__c, Type__c
                              FROM 
                                  Payments__c
                              WHERE
                                  Password__c=:prop AND Commissionable__c='Commissionable' AND Type__c='Deposit' AND Date_Recieved__c>=:lastStart AND Date_Recieved__c<:thisStart                                  
                              ORDER BY Date_Recieved__c
                              ];
        return pdlcl;
    }
    Payments__c pdlcl2;
    public Payments__c getPdlcl2(){
        pdlcl2 = new Payments__c(Payment__c = 0);
        for(Payments__c pdlc : getPaymentDepositLastComissionableList()){
        pdlcl2.Payment__c += pdlc.Payment__c; }
        return pdlcl2;  
    }
    //Non-Commissionable Bill Pay
    public Payments__c[] getPaymentBillThisNonList() {
        Payments__c[] pbtnl = [SELECT 
                                  Booking_Name_Organization__c, Commissionable__c, Date_Recieved__c, Event_Dates__c, Notes__c, Payment__c, Type__c
                              FROM 
                                  Payments__c
                              WHERE
                                  Password__c=:prop AND Commissionable__c='Non-Commissionable' AND Type__c='Bill Pay' AND Date_Recieved__c>=:thisStart AND Date_Recieved__c<:thisEnd                                  
                              ORDER BY Date_Recieved__c
                              ];
        return pbtnl;
    }
    Payments__c pbtnl2;
    public Payments__c getPbtnl2(){
        pbtnl2 = new Payments__c(Payment__c = 0);
        for(Payments__c pbtn : getPaymentBillThisNonList()){
        pbtnl2.Payment__c += pbtn.Payment__c; }
        return pbtnl2;  
    }
    //Non-Commissionable Bill Pay Last Month
    public Payments__c[] getPaymentBillLastNonList() {
        Payments__c[] pbtnl = [SELECT 
                                  Booking_Name_Organization__c, Commissionable__c, Date_Recieved__c, Event_Dates__c, Notes__c, Payment__c, Type__c
                              FROM 
                                  Payments__c
                              WHERE
                                  Password__c=:prop AND Commissionable__c='Non-Commissionable' AND Type__c='Bill Pay' AND Date_Recieved__c>=:lastStart AND Date_Recieved__c<:thisStart                                  
                              ORDER BY Date_Recieved__c
                              ];
        return pbtnl;
    }
    Payments__c pblnl2;
    public Payments__c getPblnl2(){
        pblnl2 = new Payments__c(Payment__c = 0);
        for(Payments__c pbln : getPaymentBillLastNonList()){
        pblnl2.Payment__c += pbln.Payment__c; }
        return pblnl2;  
    }
    //Non-Commissionable Deposit Pay This Month
    public Payments__c[] getPaymentDepositThisNonList() {
        Payments__c[] pdtnl = [SELECT 
                                  Booking_Name_Organization__c, Commissionable__c, Date_Recieved__c, Event_Dates__c, Notes__c, Payment__c, Type__c
                              FROM 
                                  Payments__c
                              WHERE
                                  Password__c=:prop AND Commissionable__c='Non-Commissionable' AND Type__c='Deposit' AND Date_Recieved__c>=:thisStart AND Date_Recieved__c<:thisEnd                                  
                              ORDER BY Date_Recieved__c
                              ];
        return pdtnl;
    }
    Payments__c pdtnl2;
    public Payments__c getPdtnl2(){
        pdtnl2 = new Payments__c(Payment__c = 0);
        for(Payments__c pdtn : getPaymentDepositThisNonList()){
        pdtnl2.Payment__c += pdtn.Payment__c; }
        return pdtnl2;  
    }
    //Non-Commissionable Deposits Last Month
    public Payments__c[] getPaymentDepositLastNonList() {
        Payments__c[] pdlnl = [SELECT 
                                  Booking_Name_Organization__c, Commissionable__c, Date_Recieved__c, Event_Dates__c, Notes__c, Payment__c, Type__c
                              FROM 
                                  Payments__c
                              WHERE
                                  Password__c=:prop AND Commissionable__c='Non-Commissionable' AND Type__c='Deposit' AND Date_Recieved__c>=:lastStart AND Date_Recieved__c<:thisStart                                  
                              ORDER BY Date_Recieved__c
                              ];
        return pdlnl;
    }
    Payments__c pdlnl2;
    public Payments__c getPdlnl2(){
        pdlnl2 = new Payments__c(Payment__c = 0);
        for(Payments__c pdln : getPaymentDepositLastNonList()){
        pdlnl2.Payment__c += pdln.Payment__c; }
        return pdlnl2;  
    }
    public Opportunity[] getOpportunityList() {
        Opportunity[] opps = [SELECT 
                                  Arrival_Date__c, Look_Up_Contact_Name__c, Arrive_Depart__c, Organization__c, Actual_Amount__c, System_Balance_Due__c, Property__c, Update_Required__c
                              FROM 
                                  Opportunity 
                              WHERE
                                  Password__c=:prop AND 
                                  ((StageName='Event Finalized' AND System_Balance_Due__c>0) OR
                                  ((StageName='GM' OR StageName='Anticipated' OR StageName='Projected') AND Arrival_Date__c<:tday AND Actual_Amount__c<1))
                              ORDER BY Arrival_Date__c
                              ];
        return opps;
    }
    Opportunity opp2;
    public Opportunity getOpp2(){
        opp2 = new Opportunity(Balance_Due__c = 0, Actual_Amount__c = 0, Guarenteed_Minimum_Payment__c = 0);
        for(Opportunity o : getOpportunityList()){
        opp2.Property__c = o.Property__c;
        opp2.Contract_Notes__c = ApexPages.currentPage().getParameters().get('property');
        opp2.Name = ApexPages.currentPage().getParameters().get('who');
        opp2.Balance_Due__c += o.System_Balance_Due__c; }
        return opp2;  
    }
    public pageReference generateReport() {
        PageReference goToNew = Page.ConfServices_PaymentListing;
        goToNew.getParameters().put('property', prop);
        goToNew.getParameters().put('who', whom);
        return goToNew;
    }
    public static testMethod void ConfServices_PaymentListingTest() {
       ConfServicesPaymentListing test = new ConfServicesPaymentListing();
       test.setProp('1234');
       test.setWhom('Zach');
       PageReference testPageReference = Page.ConfServices_PaymentListing;
       testPageReference.getParameters().put('property','1234');
       testPageReference.getParameters().put('who','Zach');
    system.assertEquals(testPageReference.getParameters(), test.generateReport().getParameters());
        Opportunity[] opp = [SELECT Property__c FROM Opportunity WHERE Password__c = '1234'];
        System.assertEquals('Lake Williamson', opp[0].Property__c);
}
}

When I look at the results after running the test each of the lines that start like the following two entries come out red, basically nothing will select, even through there are records to select in the sandbox and the main system. I have even tried createing records via the test, but nothing I have tried will improve these scores.

    public Payments__c[] getPaymentBillThisComissionableList() {
        Payments__c[] pbtcl = [SELECT
    Payments__c pbtcl2;
    public Payments__c getPbtcl2(){

I appreciate anyone's input.

Best Answer chosen by Admin (Salesforce Developers) 
SteveBowerSteveBower

I think you have the expectation that merely setting a pageReference to your page like you do here:

 

PageReference testPageReference = Page.ConfServices_PaymentListing;

is actually instantiating your page, and calling all your getters.  It's not.   You need to call the various functions within your controller yourself and check their results.

 

So, somewhere you need to (for example):

 

 


A) Don't use existing data in the system for your tests.... create new data.
SomeOtherData xxx = any other data you need to set up for testing....;
B) Make sure you have something that represents the correct data that you expect your getters to return.
Payments__c[] goodResult = <whatever code you need to create a list of the Payments__c which is what you want for a result.>
C) Explicitly call your getters (note: you've correctly instantiated "test" as an instance of your controller.
Payments__c[] testGPBTCList test.getPaymentBillThisComissionableList();
D) The above line will give you your code coverage, but that's pretty meaningless unless you verify
the results are correct.
system.assert(testGPBTCList, goodResult);

Do that for all your getters to make sure they are all working... 

 

Now, you may find that it's easier to partition the work... e.g. create one function to create all the test data you'll need for the whole test.

Then, you can also put a function directly in your code (as opposed to the test code) which calls all the getters one after another. 

 

Hopefully this will get you started.  Best, Steve.

 

 

 

 

All Answers

SteveBowerSteveBower

I think you have the expectation that merely setting a pageReference to your page like you do here:

 

PageReference testPageReference = Page.ConfServices_PaymentListing;

is actually instantiating your page, and calling all your getters.  It's not.   You need to call the various functions within your controller yourself and check their results.

 

So, somewhere you need to (for example):

 

 


A) Don't use existing data in the system for your tests.... create new data.
SomeOtherData xxx = any other data you need to set up for testing....;
B) Make sure you have something that represents the correct data that you expect your getters to return.
Payments__c[] goodResult = <whatever code you need to create a list of the Payments__c which is what you want for a result.>
C) Explicitly call your getters (note: you've correctly instantiated "test" as an instance of your controller.
Payments__c[] testGPBTCList test.getPaymentBillThisComissionableList();
D) The above line will give you your code coverage, but that's pretty meaningless unless you verify
the results are correct.
system.assert(testGPBTCList, goodResult);

Do that for all your getters to make sure they are all working... 

 

Now, you may find that it's easier to partition the work... e.g. create one function to create all the test data you'll need for the whole test.

Then, you can also put a function directly in your code (as opposed to the test code) which calls all the getters one after another. 

 

Hopefully this will get you started.  Best, Steve.

 

 

 

 

This was selected as the best answer
AshleyHAshleyH

Thank you! that was just what I needed.