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
JoshTonksJoshTonks 

Im trying to write a test class

Im trying to write a test class for the below code. Ive written test classes before for things but for the life of me I cannot figure out how to get this to work with it. I feel like im missing something obvious. My apex class is below. The public list is passing the rest isnt.
public with sharing class CAengineerInvoices {

  List<Service_Booking__c> CAengineerInvoices;
      Service_Booking__c NetTotal;
      Service_Booking__c GrossTotal;
    
    public List <Service_Booking__c> getCAengineerInvoices(){
      CAengineerInvoices = [SELECT   Eng_Qty__c, CA_Gross_Amount__c, CA_Gross_Total_VF__c, Description_Of_Work__c, Description_of_Additional_work__c,Service_Company_Name__c, Company_Name__c, Registration_s__c, Name, Booking_Date_Time__c, CA_Eng_Net_Price__c, KM_Over__c, CA_Tax_Amount__c,Gross_Amount__c, Booking_Date_Time_Fomrula__c
                     FROM Service_Booking__c
                     WHERE Booking_Confirmed__c = True AND Service_Company__c = :ApexPages.currentPage().getParameters().get('Id')];
      return CAengineerInvoices;
    }

	public Service_Booking__c getNetTotal() {
       NetTotal = new Service_Booking__c(CA_Eng_Net_Price__c = 0);
       
       for(Service_Booking__c CAI : getCAengineerInvoices()){
           NetTotal.CA_Eng_Net_Price__c += CAI.CA_Eng_Net_Price__c;
           }
           return NetTotal;
      }


	public Service_Booking__c getGrossTotal() {
       GrossTotal = new Service_Booking__c(CA_Gross_Total_VF__c = 0);
       
       for(Service_Booking__c GT : getCAengineerInvoices()){
           GrossTotal.CA_Gross_Total_VF__c += GT.CA_Gross_Total_VF__c;
           }
           return GrossTotal;
      }
	public Account getAccount() {
        return [SELECT Id, Name FROM Account
                WHERE Id = :ApexPages.currentPage().getParameters().get('Id')];
    }
}

 
Best Answer chosen by JoshTonks
srlawr uksrlawr uk
Looking at the code, I would imagine you are perhaps having trouble "unit testing" methods such as getNetTotal as it's hard to isolate the pre-requirements of it - ie. the list of Service_Booking__c records.

You could either add a public access modifier to the definition 
 
public List<Service_Booking__c> CAengineerInvoices

and then set it directly, or add a (testVisible only) private setter in there...
 
@testVisible
private void setInvoices(List<Service_Booking__c> CAengineerInvoices) {
    this.CAengineerInvoices = CAengineerInvoices;
}

which would let you set that list from a test method and then evaluate it's output... sort of like so:
 
@isTest
static void testGetNet() {
CAengineerInvoices cae = new CAengineerInvoices();

// public way:
cae.CAengineerInvoices = [SELECT Id, CA_Eng_Net_Price__c FROM Service_Booking__c];

// Or... private setter way:
cae.setCAengineerInvoices([SELECT Id, CA_Eng_Net_Price__c FROM Service_Booking__c]);

Test.startTest();

Service_Booking__c result = cae.getNetTotal();

Test.stopTest();'

System.assertEquals(expected, result);


}

(all this code was hand crafted in here, so is probably full of syntax errors!)

Hope that helps?

All Answers

srlawr uksrlawr uk
Looking at the code, I would imagine you are perhaps having trouble "unit testing" methods such as getNetTotal as it's hard to isolate the pre-requirements of it - ie. the list of Service_Booking__c records.

You could either add a public access modifier to the definition 
 
public List<Service_Booking__c> CAengineerInvoices

and then set it directly, or add a (testVisible only) private setter in there...
 
@testVisible
private void setInvoices(List<Service_Booking__c> CAengineerInvoices) {
    this.CAengineerInvoices = CAengineerInvoices;
}

which would let you set that list from a test method and then evaluate it's output... sort of like so:
 
@isTest
static void testGetNet() {
CAengineerInvoices cae = new CAengineerInvoices();

// public way:
cae.CAengineerInvoices = [SELECT Id, CA_Eng_Net_Price__c FROM Service_Booking__c];

// Or... private setter way:
cae.setCAengineerInvoices([SELECT Id, CA_Eng_Net_Price__c FROM Service_Booking__c]);

Test.startTest();

Service_Booking__c result = cae.getNetTotal();

Test.stopTest();'

System.assertEquals(expected, result);


}

(all this code was hand crafted in here, so is probably full of syntax errors!)

Hope that helps?
This was selected as the best answer
KevlangdoKevlangdo

CAengineerInvoices cae = new CAengineerInvoices();

either fill in the fields and do
cae.insert()

or


CAengineerInvoices cae = new CAengineerInvoices({
   field: value,
  etc....

});

cae.insert();

then select your new cae invoice and perform an assertEqual
 
JoshTonksJoshTonks
Thank you im nearly there now its just the last bit ive got to get through
public Account getAccount() {
        return [SELECT Id, Name FROM Account
                WHERE Id = :ApexPages.currentPage().getParameters().get('Id')];
}
Which i assume is because all the other things were done on service_booking__c and this is on account.
KevlangdoKevlangdo
Check out this example
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm