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
krishnagkrishnag 

test class for controller

hi i have written a controller which populates the data when it loads the page in a editable mode and when the user edits the felds and click on save the data stored in custom object.I am trying to know how can we assign test data to this controller since it is retrieved from another page. cna anybody help me in writing a test class for this scenario.

 

 

global with sharing class preferenc 
{

    public PageReference cancel() {
        PageReference p = Page.user_preference;
         p.setRedirect(true);
          return p;
    }


    

 public string guestid;
    public guestuser__c gus{get;set;}

    public preferenc() 
    {
    this.guestid=ApexPages.currentPage().getParameters().get('paramID');   
        if(guestid!=null||guestid!='')
        {
            getdetails(guestid); 
        }   

    }

    
    
    public void getdetails(string guestid)
    {
        List<guestuser__c> guests=[select id,Email__c,Phone__c,Title__c,Email_Optout__c,A_H_interests__c,After_Markets_interests__c,Construction_interests__c,D_O_interests__c,Energy_interests__c,Environmental_interests__c,Financial_Enterprises_interests__c,Healthcare_interests__c,Manufacturing_interests__c,Property_interests__c,RealEstate_interests__c,Technology_interests__c,Windstorm_notification_interests__c from guestuser__c where id=:guestid];
        if(guests.size()>0)
        {
            gus=guests[0];
        }
        else
        {
            
        }
    }
    public PageReference save()
    {
        
          update gus;
          PageReference t = Page.thanks;
          t.setRedirect(true);
          return t;  
    } 
    
    static testMethod void testpreferenc()
    {
      
     
      preferenc controller = new preferenc();
     
      controller.save();    
      controller.cancel();
    }
    
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
HarmpieHarmpie
ApexPages.currentPage().getParameters().put('paramID',<someId>);

 

You need to pass along data using the approach above

All Answers

HarmpieHarmpie
ApexPages.currentPage().getParameters().put('paramID',<someId>);

 

You need to pass along data using the approach above

This was selected as the best answer
krishnagkrishnag

u mean i need to pass the data in the test class.

HarmpieHarmpie

Yep, this is the way to pass parameters, that would normally be available by the user clicking a link or button, to your controller in testcode

krishnagkrishnag

i tried passing the id in my test class and its working fine thanks for the help