You need to sign in to do that
Don't have an account?
Adriana Reyes 7
Test class for controller with extension?
I'm trying to write a test class for a controller with an extension, but I'm not really sure where to start. I've looked at the SF documentation, but I'm not understanding. Can anyone help me with this? Thanks!
public class selectWarehouse { private final Purchase_Requisition__c pr; public string selectedWarehouse{get;set;} public selectWarehouse(ApexPages.StandardController stdController) { this.pr = (Purchase_Requisition__c)stdController.getRecord(); } public list<SelectOption> getWarehousenames() { List<SelectOption> options = new List<SelectOption>(); for (Warehouse__c w : [select name from Warehouse__c order by name desc limit 100]) { options.add(new SelectOption(w.name, w.name)); } return options; } public PageReference saveWarehouse(){ pr.Warehouse_Text__c = selectedWarehouse; update pr; return new PageReference('/'+pr.Id); } }
@istest
public class selectWarehouse_Test
{
static testmethod void validateselectWarehouse(){
Warehouse__c testWH = new Warehouse__c(Name='Test WH123');
insert testWH;
Purchase_Requisition__c testPR = new Purchase_Requisition__c(Name='Test PR123');
insert testPR;
ApexPages.StandardController sc = new ApexPages.StandardController(testPR);
selectWarehouse selectWH = new selectWarehouse(sc);
PageReference pageRef = Page.yourVFPageAssociatedWithController;
pageRef.getParameters().put('id', String.valueOf(testPR.Id));
Test.setCurrentPage(pageRef);
selectWH.getWarehousenames();
selectWH.saveWarehouse();
}
}
Please mark it best if it helps you. Thanks in advance.
Regards,
Pawan Kumar
All Answers
@istest
public class selectWarehouse_Test
{
static testmethod void validateselectWarehouse(){
Warehouse__c testWH = new Warehouse__c(Name='Test WH123');
insert testWH;
Purchase_Requisition__c testPR = new Purchase_Requisition__c(Name='Test PR123');
insert testPR;
ApexPages.StandardController sc = new ApexPages.StandardController(testPR);
selectWarehouse selectWH = new selectWarehouse(sc);
PageReference pageRef = Page.yourVFPageAssociatedWithController;
pageRef.getParameters().put('id', String.valueOf(testPR.Id));
Test.setCurrentPage(pageRef);
selectWH.getWarehousenames();
selectWH.saveWarehouse();
}
}
Please mark it best if it helps you. Thanks in advance.
Regards,
Pawan Kumar
1) https://trailhead.salesforce.com/modules/apex_testing
Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html
Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm
Sample Test Class for you
Let us know if this will help you