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
LaurenP6777LaurenP6777 

Help with Test Code for Simple VS Controller

Hi guys, 

I write test code for triggers and VS pages that actually do something. I can't seem to grasp the concept of how you write a test code for a VS page that simple displays a little table. Can someone help?
 
public class OpplineItemHist{
    private List<Oppty_Line_Item_History__c> Hist;
     private Opportunity Opp;
    public OpplineItemHist(ApexPages.StandardController controller) {
        this.Opp= (Opportunity)controller.getRecord();
        
        }
        public List<Oppty_Line_Item_History__c> getHist()
        
        {
        Hist= [SELECT Action__c,Action_Date__c,name, Action_Initiated_By__c,Action_Initiated_By__r.Alias,Fixed_Variable__c,Modification__c,
        Modification_Type__c,Opportunity__c,Oppty__c,CurrencyISOCode,Product_Family__c,mod__c,Product_Name__c,Quantity__c,Dropped__c,TotalPrice__c,Sales_Price__c,Line_Item__c,Delta_Total_Price__c,
        Site__c FROM Oppty_Line_Item_History__c WHERE Opportunity__c =:opp.id ORDER BY Action_Date__c DESC];
        
        return Hist;
        }
        }

All I do is use the above code in a VS page that displays the data. That is it!

Any help woudl be greatly appreciated. Thanks. 
Best Answer chosen by LaurenP6777
Mahesh DMahesh D
Hi LaurenP6777,

If you want to write a Test Class for the above class:

@isTest
public class OpplineItemHistTest {
      @isTest
      public static testController() {
           // Here create an opportunity record.
           Opportunity opp = new Opportunity();
           opp.Name = 'Test Opp';
           opp..CloseDate = System.today();
           opp.StageName = 'Prospect';
           insert opp;
           
           ApexPages.currentPage().getParameters().put( 'id', opp.Id );  // This is optional
           OpplineItemHist oppItemHistController = new OpplineItemHist( new ApexPages.StandardController(opp) );
           List<Oppty_Line_Item_History__c> oppLineItemHistList = oppItemHistController.getHist();
      }
}

Please do let me know if you need more information about it.

Regards,
Mahesh

All Answers

John Pipkin 14John Pipkin 14
LaurenP6777,

The best way to test this controller is to set up data in the test class; Some that meet criteria, and some that do not. Then create a new instance of the controller and use an assert on the size of the "Hist" list.

Hope that helps!
Mahesh DMahesh D
Hi LaurenP6777,

If you want to write a Test Class for the above class:

@isTest
public class OpplineItemHistTest {
      @isTest
      public static testController() {
           // Here create an opportunity record.
           Opportunity opp = new Opportunity();
           opp.Name = 'Test Opp';
           opp..CloseDate = System.today();
           opp.StageName = 'Prospect';
           insert opp;
           
           ApexPages.currentPage().getParameters().put( 'id', opp.Id );  // This is optional
           OpplineItemHist oppItemHistController = new OpplineItemHist( new ApexPages.StandardController(opp) );
           List<Oppty_Line_Item_History__c> oppLineItemHistList = oppItemHistController.getHist();
      }
}

Please do let me know if you need more information about it.

Regards,
Mahesh
This was selected as the best answer
John Pipkin 14John Pipkin 14
Oh, forgot to mention... Because "Hist" is a private properties, you will have to give it the "@TestVisible" annotation. Like so:
 
@TestVisible private List<Oppty_Line_Item_History__c> Hist;