Test Class for Standard Controller in Salesforce

Test Class for Standard Controller in Salesforce

Check the below code for writing test class for standard controller apex class.


Sample Code:
Account a = new Account( Name = ‘Test’ );   
Class_Name obj = new Class_Name( new ApexPages.StandardController( a ) ); 


here the controller is referred to a page where the standard controller is Account.

 

Sample Apex Class:
public with sharing class AccountController {  
    
    public Id accountId;
    
    public AccountController( ApexPages.StandardController controller ) {
        
        accountId = controller.getId();
        
    }  
    
}

Sample Test Class:
@isTest private class AccountControllerTest {
    
    static testMethod void testAcctCtrlr() {
        
        Account acc = new Account( Name = ‘Test’ );   
        insert acc;
        AccountController obj = new AccountController( new ApexPages.StandardController( acc ) );
        System.assertEquals( acc.Id, obj.accountId );
        
    }
    
}

Leave a Reply