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
Behzad Bahadori 18Behzad Bahadori 18 

Unit test for page reference Method

Hi how do I write a Unit test for the following Method
public String Account{get;set;}
public List<Account> AccountId{get;set;}
public Account a{get;set;}

  public PageReference step2()
    {
        
         if (Account != Null ){

    
        sandAccountId = [select Id  from Account where Name = : Account];

        for (Account a : AccountId){
        account.ParentId = a.Id;
       
            }

        }   
        qot = new Quote();
           qot.ExpirationDate = date.today() + 30;
        return Page.newOpptyStep2;
    }
FearNoneFearNone
in your test method:
1. create new class
2. set the needed data (Account, AccountId)
3. execute method
4. check content of PageReference
private static testMethod void testStep2() 
{
	// dummy data
	Account acc = new Account(Name='John');
	insert acc;
    Account accParent = new Account(Name='John');
	insert accParent;

	// create class
	YourClassName obj = new YourClassName();
    // set data
    obj.Account = acc.Name;
    obj.AccountId.add(accParent);
    // run
	PageReference pageRef = obj.step2();
    // check what you want to check
    System.assertNotEquals(null,pageRef);
    System.assertEquals('expected URL',pageRef.getUrl());
    System.assertEquals('expected ID', pageRef.get('id'));
    ...
    // refer to this:
    // https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_System_PageReference_methods.htm
}
Note: this is just a pseudocode