Tag Archives: Standard Controller

Test Class for Controller Extension

Apex Extension:

public class AccountExtension  {
    
    public Account acc {get; set;}
    
    //Constructor
    public AccountExtension(ApexPages.StandardController stdCtrl) {
        //Get Account Id
        Id accountId = stdCtrl.getId();
        
        //Get Account Required Information
        acc = [SELECT Id, Name, Business_Unit__c, Shipping_Country_Name__c FROM Account WHERE Id = : accountId];
    }
}

Apex Test Class:

@isTest()
public class TestUtils {
    
    private static testmethod  void CreateAccount(){
        
        //Create Account
        Account acc = new Account();
        acc.Name ='Test Account';
        acc.Industry = 'Retail';
        acc.AccountNumber = '123456';
        Insert acc;
        system.assertEquals(true,acc.Id != null);

        //Add Pagereference
        PageReference pageRef = Page.AccountPage;
        Test.setCurrentPage(pageRef);
        
        //Pass Standard controller Parameter
        pageRef.getParameters().put('Id', String.valueOf(acc.Id));
        String accountId = ApexPages.currentPage().getParameters().get('id');
        system.assertEquals(true, accountId != null);
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        AccountExtension accExtn = new AccountExtension(sc);
    }
}