+ Start a Discussion
BridgetreeBridgetree 

controller for my class

 

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.Validaion__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();
    }

}


Best Answer chosen by Admin (Salesforce Developers) 
Ankit AroraAnkit Arora

Just to get an idea , go with this :

 

@isTest
private class MyTestClass
{
    private static testmethod void test1()
    {
	editProductController controller = new editProductController() ;
	PageReference pg1 = controller.ViewData() ;
	PageReference pg2 = controller.showSelected() ;
	Integer totalNumber = controller.getTotalPageNumber() ;
	Integer PN = controller.getPageNumber() ;
	BC_Product__c BCP = controller.getProducts() ;
	List<BC_Product__c> lst = controller.getAccounts() ;
	Integer PS = controller.getPageSize() ;
	Boolean prNum = controller.getPreviousButtonEnabled() ;
	Boolean nxtNum = controller.getNextButtonDisabled() ;
	
	if(totalNumber > 0)
	{
		PageReference pg3 = controller.nextBtnClick() ;
	}
	if(totalNumber > 0)
	{
		PageReference pg3 = controller.previousBtnClick() ;
	}
    }
}

 

You can go with other method/properties of class in the same way. Let me know if there are any issues.

 

Thanks

Ankit Arora

 Blog | Facebook | Blog Page

All Answers

Ankit AroraAnkit Arora

Just to get an idea , go with this :

 

@isTest
private class MyTestClass
{
    private static testmethod void test1()
    {
	editProductController controller = new editProductController() ;
	PageReference pg1 = controller.ViewData() ;
	PageReference pg2 = controller.showSelected() ;
	Integer totalNumber = controller.getTotalPageNumber() ;
	Integer PN = controller.getPageNumber() ;
	BC_Product__c BCP = controller.getProducts() ;
	List<BC_Product__c> lst = controller.getAccounts() ;
	Integer PS = controller.getPageSize() ;
	Boolean prNum = controller.getPreviousButtonEnabled() ;
	Boolean nxtNum = controller.getNextButtonDisabled() ;
	
	if(totalNumber > 0)
	{
		PageReference pg3 = controller.nextBtnClick() ;
	}
	if(totalNumber > 0)
	{
		PageReference pg3 = controller.previousBtnClick() ;
	}
    }
}

 

You can go with other method/properties of class in the same way. Let me know if there are any issues.

 

Thanks

Ankit Arora

 Blog | Facebook | Blog Page

This was selected as the best answer
BridgetreeBridgetree

Thanks Ankit Sir...

 

Had to do some changes.. achieved the required result. and this has given me a right way to write the test cases.

I have learnt to write it.

 

Thank u.

Thanks alot..

 

 

Ankit AroraAnkit Arora

That's great, and glad that you got it. And please do not call me "Sir" :P

And yeah if solution helped you then can you please mark it as solution so others may get benefit.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page