You need to sign in to do that
Don't have an account?

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.
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.
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
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/
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
OpportunityTermSheetCtlr ext = new OpportunityTermSheetCtlr(stdController);
Same error i am getting.
Thanks
kumar
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();
}
}
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.