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
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12 

how to write a Test class for extension

Hi all,
write a test class for extension Controller. 

public with sharing class OpportunityTermSheetCtlr {

public List<Custom_Asset__c> listActiveAssets {get;set;}
public List<OpportunityLineItem> listOpportunityLineItem {get;set;}
public Opportunity o;
public OpportunityTermSheetCtlr(ApexPages.StandardController controller)
{
this.o = (Opportunity) controller.getRecord();

listActiveAssets = [select End_User__c, Product_Name__c, Implementation_Method__c, Production_Version__c, UOM_Quantity__c
                    from Custom_Asset__c where End_User__c = :o.End_User__c AND Active__c = 'Yes'];
  
listOpportunityLineItem = [select OpportunityId, Product_Name__c, QBR_RevenueType__c, UOM_Qty__c, UOM_List_Unit_Price__c, UOM_Net_Unit_Price__c, DiscountPercentTotalUOM__c, TotalPrice
                        from OpportunityLineItem where OpportunityId = :o.id];
                    
}
}

Thanks in adv.
Best Answer chosen by kumar.fdc81.3902978579608325E12
Subramani_SFDCSubramani_SFDC
@isTest
private class OpportunityTermSheetCtlrTestclass {
     static testMethod void myUnitTest() {
  
        Account a = new Account(Name='OpportunityPipelineChangeTest',Geography__c='TestGeography',Territory_Id__c='TestTerritory',
            Territory_Overlay__c='TestTerritoryOverlay',Renewals_Team__c='TestRenewalsTeam',Renewals_Account_Manager__c='TestRenewalsAccountManager');
        insert a;
        Contact c = new Contact(AccountId=a.id,LastName='Test',Job_Function__c='TestJobFunction',Department__c='TestDepartment');
        insert c;
        Opportunity O = new Opportunity(Name='TestPipelineChange',AccountId=a.id,End_User__c=a.id,
            Contact__c=c.id,CloseDate=System.today(),Type='New Customer',Order_Type__c='New',
            Lead_Source_Category__c='Existing Customer',StageName='Create',Primary_Business_Driver__c='Test');
        insert O;
Test.Starttest();
      
        ApexPages.StandardController stdController = new ApexPages.StandardController(O);
OpportunityTermSheetCtlr ext = new OpportunityTermSheetCtlr(stdController);
Test.Stopest();
  
    }
  
}

All Answers

Subramani_SFDCSubramani_SFDC
try like this
//Lets Assume we are writing Controller Extension for Account
Account acct = [SELECT ID FROM Account LIMIT 1];

//Start Test Context, It will reset all Governor limits
Test.startTest();

//Inform Test Class to set current page as your Page where Extension is used
Test.setCurrentPage(Page.YOUR_PAGE);

//Instantiate object of "ApexPages.StandardController" by passing object
ApexPages.StandardController stdController = new ApexPages.StandardController(acct);

//Now, create Object of your Controller extension by passing object of standardController
YOUR_Extension ext = new YOUR_Extension(stdController);

//Here you can test all public methods defined on Extension "ext"
//..... your code

//Finish Test
Test.stopTest();
in your scenario you should insert records for opportunity,opplineitem,Custom_Asset__c

replace Account by opplineitem in the above test class

for more ref :http://www.shivasoft.in/blog/salesforce/apex/faq-writing-test-class-in-salesforce/
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Thanks Subra,

I written test class like this 
@isTest
private class OpportunityTermSheetCtlrTestclass { 
     static testMethod void myUnitTest() {
    
       OpportunityTermSheetCtlr opp = new OpportunityTermSheetCtlr ();

        Account a = new Account(Name='OpportunityPipelineChangeTest',Geography__c='TestGeography',Territory_Id__c='TestTerritory',
            Territory_Overlay__c='TestTerritoryOverlay',Renewals_Team__c='TestRenewalsTeam',Renewals_Account_Manager__c='TestRenewalsAccountManager');
        insert a;
        Contact c = new Contact(AccountId=a.id,LastName='Test',Job_Function__c='TestJobFunction',Department__c='TestDepartment');
        insert c;
        Opportunity O = new Opportunity(Name='TestPipelineChange',AccountId=a.id,End_User__c=a.id,
            Contact__c=c.id,CloseDate=System.today(),Type='New Customer',Order_Type__c='New',
            Lead_Source_Category__c='Existing Customer',StageName='Create',Primary_Business_Driver__c='Test');
        insert O;
       
        ApexPages.StandardController stdController = new ApexPages.StandardController(a);
   
    }
   
}


Getting this error

Constructor not defined: [OpportunityTermSheetCtlr].<Constructor>() in salesforce

Thanks in adv

Subramani_SFDCSubramani_SFDC
Add this line
OpportunityTermSheetCtlr ext = new OpportunityTermSheetCtlr(stdController);
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Hi subra,

Same error i am getting.

Thanks
kumar
Subramani_SFDCSubramani_SFDC
@isTest
private class OpportunityTermSheetCtlrTestclass {
     static testMethod void myUnitTest() {
  
        Account a = new Account(Name='OpportunityPipelineChangeTest',Geography__c='TestGeography',Territory_Id__c='TestTerritory',
            Territory_Overlay__c='TestTerritoryOverlay',Renewals_Team__c='TestRenewalsTeam',Renewals_Account_Manager__c='TestRenewalsAccountManager');
        insert a;
        Contact c = new Contact(AccountId=a.id,LastName='Test',Job_Function__c='TestJobFunction',Department__c='TestDepartment');
        insert c;
        Opportunity O = new Opportunity(Name='TestPipelineChange',AccountId=a.id,End_User__c=a.id,
            Contact__c=c.id,CloseDate=System.today(),Type='New Customer',Order_Type__c='New',
            Lead_Source_Category__c='Existing Customer',StageName='Create',Primary_Business_Driver__c='Test');
        insert O;
Test.Starttest();
      
        ApexPages.StandardController stdController = new ApexPages.StandardController(O);
OpportunityTermSheetCtlr ext = new OpportunityTermSheetCtlr(stdController);
Test.Stopest();
  
    }
  
}
This was selected as the best answer
kumar.fdc81.3902978579608325E12kumar.fdc81.3902978579608325E12
Thanks subra. It's working fine.
Ravikant Ravikant 10Ravikant Ravikant 10
Hi All,

I have written an extension class and it's class but my test class is not passing the last two lines of my extension class. I am getting the error "System.QueryException: List has no rows for assignment to SObject".

Extension class:
public class BusinessPlanRecords {

    public List<Business_Plan__c> Records{get;set;}
   
    public BusinessPlanRecords(ApexPages.StandardController controller){
        
        Records = new List<Business_Plan__c>();
        String currentRecordId;
        Territory_Sales_Plan__c tsmPlan;
        currentRecordId = ApexPages.CurrentPage().getparameters().get('id');
        tsmPlan = [SELECT Id,Year__c,Territory__c FROM Territory_Sales_Plan__c WHERE Id =: currentRecordId Limit 1];
        
        List<Account> acctList =[SELECT Id FROM Account WHERE Territory__c =: tsmPlan.Territory__c];
        Records = [SELECT Id,Name,Town__c,Year__c,Account__r.Name FROM Business_Plan__c WHERE Account__c IN:acctList AND Show_in_TSM_Plan__c = True AND Name =: tsmPlan.Year__c] ;
        
    }
}



Test Class:
@isTest
Private class TestBusinessPlanRecords{
    
    static testMethod void unitTestBusinessPlanRecords(){
        
        Test.Starttest();
        
        Territory__c territory = new Territory__c(Name='Territory1',Area_Sales_Manager__c='00590000001BXP1',Business_controller__c='00590000001B16V',CU_Manager__c='00590000001CYeT',Commercial_Unit__c='CENTRAL');
        insert territory;
        
        Post_Code__c postCode = new Post_Code__c(Name='12567',Town__c='BOOMI',State__c='NSW',Country__c='Australia',Territory__c=territory.Id);
        insert postCode;
        
        Account acct = new Account(RecordTypeId='01290000000SKlw',Name='Account1',Post_Code__c=postCode.Id,Territory__c=territory.Id,Phone='+61 (12) 12345678');
        insert acct;
        
        Business_Plan__c businessPlan = new Business_Plan__c(Name='2017',RecordTypeId='01290000000SKm7',Account__c=acct.Id,Show_in_TSM_Plan__c= True);
        insert businessPlan;
        
        Territory_Sales_Plan__c tsmPlan = new Territory_Sales_Plan__c(Territory__c=territory.Id,Year__c='2017');
        insert tsmPlan;
      
        ApexPages.StandardController stdController = new ApexPages.StandardController(tsmPlan);
        BusinessPlanRecords ext = new BusinessPlanRecords(stdController);
        
        Test.Stoptest();
    } 
}




Thanks in advance.