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
BridgetreeBridgetree 

test class for my controller

Can anyone help me in writing a test class for my controller.

 

The controller is as follows

 

 

public class editProductController {
    private List<BC_Product__c> accounts;
    private List<BC_Product__c> pageAccounts;
    private Integer pageNumber;
    private Integer pageSize;
    private Integer totalPageNumber;
    public boolean testBool {get; set;}
    public string chkText {get; set;}
    public List<BC_Product__c> PDTS {get; set;}
    public BC_Product__c JEPS;
    public string selectedOptions {get;set;}
    BC_Product__c products;
    
    public editProductController(){    
        pageNumber = 0;
        totalPageNumber = 0;
        pageSize = 75;
        ViewData();
        JEPS = new BC_Product__c();
        PDTS = new List<BC_Product__c>();
        PDTS.add(new BC_Product__c());
        testBool = false;
    }
    
    public Integer getPageNumber(){
        return pageNumber;
    }

    public BC_Product__c getProducts(){
        if(products == null) products = new BC_Product__c();
            return products;
    }

    public List<BC_Product__c> getAccounts() {
        return pageAccounts;
    }

    public Integer getPageSize(){
        return pageSize;
    }

    public Boolean getPreviousButtonEnabled(){
        return !(pageNumber > 1);
    }

    public Boolean getNextButtonDisabled(){
        if (accounts == null) 
            return true;
        else
            return ((pageNumber * pageSize) >= accounts.size());
    }

    public Integer getTotalPageNumber(){
        if (totalPageNumber == 0 && accounts !=null) {
            totalPageNumber = accounts.size() / pageSize;
            Integer mod = accounts.size() - (totalPageNumber * pageSize);    
            if (mod > 0)
                totalPageNumber++;
        }
        return totalPageNumber;
    }

    public PageReference ViewData(){
        accounts = null;
        totalPageNumber = 0;
        BindData(1);
        return null;
    }

    private void BindData(Integer newPageIndex){
        try{
            if (accounts == null)
                accounts = [select b.name,b.Validation__c,b.Product_Description__c,b.Status__c,b.JEP__c,b.Disconnect_request_Sent_to_Vendor__c,b.Vendor_Bill_Start_Date__c,
                b.Actual_Disconnect_Date__c,b.Date_Entered_Provisioning__c,b.CR_Install_Date__c,b.Customer_Commit_Date__c,b.Vendor_CDD__c,
                b.SUP_Date__c,b.Actual_Install_Date__c,b.Cancel_Request_Recieved__c,b.CR_Disconnect_Date__c,b.Disconnect_Date__c,b.Disconnect_Reason__c,
                b.Disconnect_Reason_Notes__c from BC_Product__c b where Account__c=:System.currentPageReference().getParameters().get('id') order by name];
            
            if(accounts == null || accounts.size() <= 0)
                accounts = [select b.name,b.Validation__c,b.Product_Description__c,b.Status__c,b.JEP__c,b.Disconnect_request_Sent_to_Vendor__c,b.Vendor_Bill_Start_Date__c,
                b.Actual_Disconnect_Date__c,b.Date_Entered_Provisioning__c,b.CR_Install_Date__c,b.Customer_Commit_Date__c,b.Vendor_CDD__c,
                b.SUP_Date__c,b.Actual_Install_Date__c,b.Cancel_Request_Recieved__c,b.CR_Disconnect_Date__c,b.Disconnect_Date__c,b.Disconnect_Reason__c,
                b.Disconnect_Reason_Notes__c from BC_Product__c b where Opportunity__c=:System.currentPageReference().getParameters().get('id') order by name];
        
            pageAccounts = new List<BC_Product__c>();
            Transient Integer counter = 0;
            Transient Integer min = 0;
            Transient Integer max = 0;
            if (newPageIndex > pageNumber){
                min = pageNumber * pageSize;
                max = newPageIndex * pageSize;
            }
            else {
                max = newPageIndex * pageSize;
                min = max - pageSize;
            }
            for(BC_Product__c a : accounts){   
                counter++;
                if (counter > min && counter <= max)
                    pageAccounts.add(a);
            }
    
            pageNumber = newPageIndex;
            if (pageAccounts == null || pageAccounts.size() <= 0)
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Data not available for this view.'));
        }catch(Exception ex){
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,ex.getMessage()));
        }
    }

    public PageReference nextBtnClick(){
        BindData(pageNumber + 1);
        return null;
    }

    public PageReference previousBtnClick(){
        BindData(pageNumber - 1);
        return null;
    }
    
    public PageReference Save(){
        update pageAccounts;
        return ApexPages.currentPage();
    }

    public BC_Product__c getJEPS(){
       return JEPS;
    }

    public void setJEPS(BC_Product__c Value){
       JEPS = Value;
    }

    public PageReference showSelected(){    
       if (JEPS.JEP__c == null || JEPS.JEP__c == ''){
          apexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'Please select one or more products.'));
          return null;
       }
       // read the values into an array
       string[] regions = JEPS.JEP__c.split(',',0);
       if (regions != null && regions.size() > 0){
          Boolean addSeperater = false;
          for(String le : regions){
                if(addSeperater)
                  selectedOptions = selectedOptions + ',' + le;
                else{
                   selectedOptions = le;
                   addSeperater = true;
                }
          }
       } 
       return ApexPages.currentPage();
    }

}

Chris JohnChris John

Some helpful links from the docs:

Testing Custom Controllers and Controller Extensions: http://www.salesforce.com/us/developer/docs/pages/index_Left.htm#StartTopic=Content/pages_controller_error_handling.htm?SearchType=Stem

 

Testing Apex: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_testing.htm

 

The general Apex testing link contains some helpful info on best practices.

 

Have a go at writing the test class and if there is any code in particular that is hard to write tests for, let us know.

BridgetreeBridgetree

i went through all those pages. I am tryin out to write my test class. But i'm not able to figure out how to write a test class because i have never written. I am new to this world. Pls pls teach me to write the test classes by giving the test class for the above controller. There is a id field that has to be taken in the controller. I am not able to sort it out ini the test class.

 

I am in much urgent for this test class. Please anyone help me out.

 

Raj

BridgetreeBridgetree

someone provide me the test class for this controller. I am not able to write it