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
Ishan K SharmaIshan K Sharma 

Newbie to unit testing

Hi

I am new to test classes. I have made controller which contain only querries. I need help to make a test class of it.

what should be the approach?

 

///////////////////////controller///////////////////////////////////////

 

public with sharing class Bcaformatcontroller{

public String workstations { get; set; }
public date today{get;set;}
public String offerID = ApexPages.currentPage().getParameters().get('Id');
public List <RealSteer__LeaseOffer__c> records {get;set;}
public List<Contact> licenseeCon {get;set;}
public List<AccountContactRole> licenseeConID {get;set;}
public List<AccountContactRole> licensorConID {get;set;}
public List<Contact> licensorCon {get;set;}
public List<Services__c> charge{get; set;}
public List<Services__c> chargeParking{get; set;}
public String pdfURL{get;set;}

public Bcaformatcontroller(){
today = System.today();
records = [Select r.Deposit_ask_for__c, r.Start_Date__c, r.RealSteer__Property__r.Name,RealSteer__Property__r.RealSteer__Street__c,RealSteer__Property__r.RealSteer__City__c,
RealSteer__Property__r.RealSteer__State__c,RealSteer__Property__r.RealSteer__Country__c, Offer_Through__c, RealSteer__Property__r.Account__c,
r.RealSteer__Property__c, r.Name, r.Id, r.End_Date1__c, r.Duration__c,Company_Name__r.Name,r.RealSteer__Property__r.Total_L_Shaped__c, r.RealSteer__Property__r.Total_Linear__c,
r.Company_Name__c, Broker_Name__c,Company_Name__r.BillingCountry,Company_Name__r.BillingPostalCode,
Company_Name__r.BillingState,Company_Name__r.BillingCity,Company_Name__r.BillingStreet ,r.Lock_In_Period__c, r.Notice_Period__c
From RealSteer__LeaseOffer__c r where id = :offerID];

licenseeConID = [SELECT ContactId FROM AccountContactRole WHERE (IsPrimary =: true AND AccountId =: records[0].Company_Name__c)];
licenseeCon = [Select LastName,Phone,Fax, FirstName, Salutation From contact where id =: licenseeConID[0].ContactId ];

licensorConID = [SELECT ContactId FROM AccountContactRole WHERE (IsPrimary =: true AND AccountId =: records[0].RealSteer__Property__r.Account__c)];
licensorCon = [Select LastName,Phone,Fax, FirstName, Salutation From contact where id =: licensorConID[0].ContactId ];

charge = [Select Charges_Per_Hour__c from Services__c where (Business_Center_Name__c = :records[0].Company_Name__c AND RecordTypeid in(select id from recordtype where name = : 'Internet Services'))];
chargeParking = [Select Parking_Rate__c from Services__c where (Business_Center_Name__c = :records[0].Company_Name__c AND RecordTypeid in(select id from recordtype where name = : 'Parking'))];


}


}

 

Thanks

Ishan Sharma

Best Answer chosen by Admin (Salesforce Developers) 
vbsvbs

Ishan - Here is a rough template:

1. Setup test data for the org for all the queried objects

2. Set current visualforce page using Test.system.currentpageReference

3. Set up offerid parameter for the page

4. Instantiate object of controller class

5. Assert record counts and expected positive/negative test cases

All Answers

vbsvbs

Ishan - Here is a rough template:

1. Setup test data for the org for all the queried objects

2. Set current visualforce page using Test.system.currentpageReference

3. Set up offerid parameter for the page

4. Instantiate object of controller class

5. Assert record counts and expected positive/negative test cases

This was selected as the best answer
Ashish_SFDCAshish_SFDC

Hi Ishan,

 

Basically your test class should

  1. add a dummy test record
  2. save that test record to a list 
  3. query using your Class
  4. Match the record queried and the one already saved in the List - Assertion Succeeds.

Regards,

Ashish

Ishan K SharmaIshan K Sharma

Thanks to both of you . This was helpful. I am done with my test class.

vbsvbs
Please mark this as a solution so others can refer back to this in the future.
Ashish_SFDCAshish_SFDC

Hi Ishan, 

 

Great to know, you are welcome!

 

Regards,

Ashish