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
Frank CarterFrank Carter 

apex test class controller

Hello,
I need help. I created a vf page and a controller but now I Have to do the apex test class. This is the first time that I have to do an apex test class for a controller and although I have seen many post and read trailheads I don't understand how to do.

vf page:
<apex:page controller="BDPaginationController" >
    <apex:form id="theform" >
        <apex:actionFunction name="refreshPageSize" action="{!refreshPageSize}" status="fetchStatus" reRender="pbId"/>
        <apex:pageBlock id="pbId" title="Billing Details of this Month">
            <apex:pageBlockSection collapsible="false" columns="1">
                <apex:pageBlockTable value="{!BD}" var="b">
                    <apex:column >
                   <apex:outputLink title="" value="/{!b.Id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold">Edit</apex:outputLink>&nbsp;|&nbsp;
                    <apex:commandLink style="font-weight:bold" action="{!deleteBD}" onclick="if(!confirm('Are you sure you want to delete this record?')) return false;" >Del <apex:param value="{!b.Id}" name="idToDel" assignTo="{!SelectedBdId}"/> </apex:commandLink>&nbsp;|&nbsp;
                    
                    </apex:column>
                    <apex:column value="{!b.Name}"/>
                    <apex:column value="{!b.SF_Opportunity_Id__c}"/>
                    <apex:column value="{!b.Billing_Type__c}"/>
                    <apex:column value="{!b.billing_period__c}"/>    
                    <apex:column value="{!b.Monthly_Forecast__c}"/>
                    <apex:column value="{!b.End_of_Billing__c}"/>
                    <apex:column value="{!b.Amount__c}"/>
                    <apex:column value="{!b.Billing_Status__c}"/>
                    <apex:column value="{!b.Position__c}"/>
                    <apex:column >
                    <apex:commandLink style="font-weight:bold" action="{!BilledBD}" >Billed <apex:param value="{!b.Id}" name="idToBil" assignTo="{!SelectedBdId}"/> </apex:commandLink>
                    </apex:column>
                </apex:pageBlockTable>
                 
                <apex:panelGrid columns="8"> 
                 
                <apex:selectList value="{!size}" multiselect="false" size="1" onchange="refreshPageSize();">
                    <apex:selectOptions value="{!paginationSizeOptions}"/>
                </apex:selectList>
                 
                <apex:commandButton status="fetchStatus" reRender="pbId" value="First" action="{!setCon.first}" disabled="{!!setCon.hasPrevious}" title="First Page"/> 
  
                <apex:commandButton status="fetchStatus" reRender="pbId" value="Previous" action="{!setCon.previous}" disabled="{!!setCon.hasPrevious}" title="Previous Page"/> 
  
                <apex:commandButton status="fetchStatus" reRender="pbId" value="Next" action="{!setCon.next}" disabled="{!!setCon.hasNext}" title="Next Page"/> 
  
                <apex:commandButton status="fetchStatus" reRender="pbId" value="Last" action="{!setCon.last}" disabled="{!!setCon.hasNext}" title="Last Page"/> 
  
                <apex:outputText >{!(setCon.pageNumber * size)+1-size}-{!IF((setCon.pageNumber * size)>noOfRecords, noOfRecords,
                     (setCon.pageNumber * size))} of {!noOfRecords}
                </apex:outputText> 
                       
                <apex:outputPanel >                      
                    <apex:actionStatus id="fetchStatus" >
                        <apex:facet name="start" >
                          <img src="/img/loading.gif" />                    
                        </apex:facet>
                    </apex:actionStatus>
                </apex:outputPanel> 
  
            </apex:panelGrid>  
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton value="Moves to next month" onclick="if(!confirm('If you click ok all the billing details you are viewing will be paid for next month. Are you sure? ')) return false;" action="{!myAction}" />   
            </apex:pageBlockButtons>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

controller:
public class BDPaginationController{
    Public Integer size{get;set;} 
    Public Integer noOfRecords{get; set;} 
    public List<SelectOption> paginationSizeOptions{get;set;}
         
    public BDPaginationController(){
        size=80;
        paginationSizeOptions = new List<SelectOption>();
        paginationSizeOptions.add(new SelectOption('20','20'));
        paginationSizeOptions.add(new SelectOption('50','50'));
        paginationSizeOptions.add(new SelectOption('80','80'));
        paginationSizeOptions.add(new SelectOption('120','120'));
        paginationSizeOptions.add(new SelectOption('200','200'));
    }
     
    public ApexPages.StandardSetController setCon {
        get {
            if(setCon == null) {                
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(
                      [Select Id,SF_Opportunity_Id__c, Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c, Position__c   from Billing_Detail__c Where Monthly_Forecast__c=THIS_MONTH Order by Monthly_Forecast__c]));
                setCon.setPageSize(size);  
                noOfRecords = setCon.getResultSize();
            }            
            return setCon;
        }
        set;
    }
     
    //Changes the size of pagination
    public PageReference refreshPageSize() {
         setCon.setPageSize(size);
         return null;
    }
 
    // Initialize setCon and return a list of record    
     
    public List<Billing_Detail__c> getBD() {
         return (List<Billing_Detail__c>) setCon.getRecords();
    }
    

    public List<Billing_Detail__c> listBD {get;set;}
    public String SelectedBdId {get;set;}
            
    public void loadData() {
        listBD = [Select Id,SF_Opportunity_Id__c,Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c, Position__c   from Billing_Detail__c Where Monthly_Forecast__c=THIS_MONTH ];
    }

//to delete
    public PageReference deleteBD(){
      DELETE new Billing_Detail__c (Id = SelectedBdId);
      PageReference pageRef = ApexPages.currentPage();
      pageRef.setRedirect(true);
      return pageRef;
   }
 
 //to move monthly forecast
 	public PageReference myAction(){
		MoveBD1.Move1();
        PageReference pageRef = ApexPages.currentPage();
      	pageRef.setRedirect(true);
      	return pageRef;
	}
    
    //to put in billed 
    public PageReference BilledBD(){
       Billing_Detail__c bil= new Billing_Detail__c (Id = SelectedBdId);
       bil.Billing_Status__c='Billed';
       update bil;
       PageReference pageRef = ApexPages.currentPage();
       pageRef.setRedirect(true);
      return pageRef;
   }
   
}
apex class method:
public class MoveBD1 {
    
     public static void Move1(){
        
        List <Billing_Detail__c> listBill1= new List <Billing_Detail__c>();
       
        
        
       listBill1 = [Select Id,Billing_Detail__c,SF_Opportunity_Id__c,Name,Billing_Type__c, Billing_Period__c , Monthly_Forecast__c,End_of_Billing__c, Amount__c, Billing_Status__c   from Billing_Detail__c Where Monthly_Forecast__c=THIS_MONTH];
       System.debug('list '+listBill1);
        for(Billing_Detail__c b1: listBill1){
            System.debug('monthly forecast b1'+b1.Monthly_Forecast__c);
            
           
            	if(b1.Billing_Status__c!='Billed'){
                	b1.Monthly_Forecast__c=  b1.Monthly_Forecast__c.addMonths(1);
                    System.debug('monthly forecast b1 after '+b1.Monthly_Forecast__c);
                    
            	}
            
        	
    	}
        update listBill1;
    } 

}

Can someone help me??

Thanks,
Frank
Best Answer chosen by Frank Carter
Steven NsubugaSteven Nsubuga
Here you go.
@isTest
private class BDPaginationController2Test {
	
	@isTest static void testController() {
        
        Test.startTest();      
        
		// create the object in input of the class 
		Account acct = new Account(Name = 'Test Account');
		insert acct;

		Opportunity testOpportunity = new Opportunity(
		AccountID = acct.Id,
		Name = 'Test Opportunity',
		StageName = 'Needs Analysis',
		CloseDate = date.today().addDays(15)        

		);
		insert testOpportunity;

		Billing_Detail__c c= new Billing_Detail__c(); 
		c.Amount__c= 100;
		c.Billing_Type__c='FEE';
		c.Billing_Period__c='monthly';
		c.Billing_Status__c='Authorized for Billing'; 
		c.Product_Line__c='Interactive Experience (iX)'; 
		c.Product__c='Chat Delivery'; 
		c.Monthly_Forecast__c=Date.newInstance(2018, 7, 9); 
		c.End_of_Billing__c=Date.newInstance(2019, 7, 9); 
		c.Billing_Detail__c=testOpportunity.Id;

		insert c;
		
        // replace bd with the name of your visualforce page
        PageReference pageRef = Page.bd;
        Test.setCurrentPage(pageRef);
        
		BDPaginationController controller= new BDPaginationController();
        controller.refreshPageSize();
        
        List<Billing_Detail__c> bds = controller.getBD();
        controller.loadData();
        
        controller.myAction();
        controller.SelectedBdId = c.Id;
        controller.BilledBD();
        controller.deleteBD();
        
        Test.stopTest();
	}
}