+ Start a Discussion
Adriana Reyes 7Adriana 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);
    }
}

 
Best Answer chosen by Adriana Reyes 7
PawanKumarPawanKumar
Plese, try below code. Before trying please repace yourVFPageAssociatedWithController with your actual VF page name.

@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

PawanKumarPawanKumar
Plese, try below code. Before trying please repace yourVFPageAssociatedWithController with your actual VF page name.

@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
This was selected as the best answer
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
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
@istest
public class selectWarehouseTest
{
    static testmethod void validateselectWarehouse(){
        Warehouse__c wh = new Warehouse__c();
		wh,Name='Test';
        insert wh;

        Purchase_Requisition__c pr = new Purchase_Requisition__c();
		pr.Name='Test';
        insert pr;

        ApexPages.StandardController sc = new ApexPages.StandardController(pr);
        selectWarehouse obj = new selectWarehouse(sc);

        list<SelectOption> lst =obj.getWarehousenames();
		System.assert(lst != null);
		obj.selectedWarehouse ='Test';
        obj.saveWarehouse();
		
      }
      
}

Let us know if this will help you