+ Start a Discussion
Salah Odeh 10Salah Odeh 10 

Why My Test class is not covering my Apex Controller

Hello,

I have the following Visualforce page along wiht Apex Controller and Test Class. When I run the test class the coverage for Apex Controller is 0%, any idea on how to solve this issue:

Visualforce Page:

<apex:page standardController="Bid_Summaries__c" recordSetVar="Bid_Summaries__c" extensions="BidSummariesController"> <apex:pageBlock title="Bid Summary"> <apex:slds /> <style type="text/css"> body {background-color: white;} p { font-weight: bold; } p { font-style: italic;} h1 { color: #f00; } p { background-color: #eec; } newLink { color: #f60; font-weight: bold; } </style> <apex:page lightningStylesheets="true"/> <apex:form > <apex:pageBlockTable value="{! Bid_Summaries__c }" var="b"> <apex:column value="{! b.Name }"/> <apex:column value="{! b.Original_Budget__c }"/> <apex:column value="{! b.Upgrade_Amount__c }"/> <apex:column value="{! b.Budget_incl_Upgrades__c }"/> <apex:column value="{! b.TJH_Service_Fee__c }"/> <apex:column value="{! b.Concession__c }"/> <apex:column value="{! b.Upgrade_Total__c}"/> </apex:pageBlockTable> </apex:form> </apex:pageBlock> </apex:page>

Apex Controller:

​​​​​​public class BidSummariesController {

    public BidSummariesController(ApexPages.StandardController controller) {

    }

        public List<Bid_Summaries__c> Bid {get; set;}
        public BidSummariesController(){
        List<Bid_Summaries__c> Bid= [Select Id, Selected_Option__c,Concession__c, Status__c,TJH_Service_Fee__c,
                                    Upgrade_Amount__c,Upgrade_Total__c, Original_Budget__c From Bid_Summaries__c] ;
    }
                       PageReference pageRef = Page.BidSummary1;                    
                       public list <Bid_Summaries__c> getBid(){
                                              
                       return Bid;
                       
        }
    
    }


Test Class:

@isTest 

    public class BidSummariesControllertest {
        
        private static testMethod void BidSummariesController() {
            
         test.StartTest();
            
         Bid_Summaries__c b = new Bid_Summaries__c();
         
         b.Upgrade_Amount__c     = 1000;
         b.Concession__c         = 1000;         
         b.Original_Budget__c = 100;
         b.Status__c  = 'New';
            
            Insert b;
            
         
          
           test.stopTest(); 
        PageReference pageRef = Page.BidSummary1;
        Test.setCurrentPage(pageRef);
        ApexPages.StandardController sc = new ApexPages.StandardController(b);
        
      
             
           
             
            List<Bid_Summaries__c> Bids = [Select Id, Selected_Option__c,Concession__c, Status__c,TJH_Service_Fee__c,
                                    Upgrade_Amount__c,Upgrade_Total__c, Original_Budget__c From Bid_Summaries__c ];
                    
                    Update Bids;
            
            
            
        } 
        
        List<Bid_Summaries__c> Bids = New List<Bid_Summaries__c>();
             
               
                                    
          
        }
shaik murthujavalishaik murthujavali
Hi Salah Odeh,

In your test class-->
--  Initially create test data (create bid summary record)-- 
-->
ApexPages.StandardController sc = new ApexPages.StandardController(**pass bid summary record here**);
        BidSummariesController test = new BidSummariesController (sc);

 PageReference pageRef = Page.BidSummary1;
        pageRef.getParameters().put('id', String.valueOf(**pass bid summary record Id here**));
        Test.setCurrentPage(pageRef);
call all methods.

Thanks,
Shaik Murthujavali
Salah Odeh 10Salah Odeh 10
Hi Shaik,

Thanks for the reply, when you say pass bid summary record are you refering to a field value, can you give an example. also when you say pass bid summary record Id are you refering to the actual record Id ex (a6C1A000000g7DhUOI)
Salah Odeh 10Salah Odeh 10
I'm only getting 25% coverage with this test class:

@isTest 

    public class BidSummariesControllertest {
        
        private static testMethod void BidSummariesController() {
            
         test.StartTest();
            
         Bid_Summaries__c b = new Bid_Summaries__c();
            
             b.Selected_Option__c   = true;
             b.Upgrade_Amount__c     = 1000;
             b.Concession__c         = 1000;         
             b.Original_Budget__c = 100;
             b.Status__c  = 'New';
                
            Insert b; 
           List<Bid_Summaries__c> Bids = [Select Id, Selected_Option__c,Concession__c, Status__c,TJH_Service_Fee__c,
                                    Upgrade_Amount__c,Upgrade_Total__c, Original_Budget__c From Bid_Summaries__c ];
                    
                    Update Bids;  
          

            
           test.stopTest();
           
        
                    
                   
         
        ApexPages.StandardController sc = new ApexPages.StandardController(b);
        BidSummariesController test = new BidSummariesController (sc);
        PageReference pageRef = Page.BidSummary1;
        pageRef.getParameters().put('id', String.valueOf(b));
        
          
        }
    }

Apex controller: 

public class BidSummariesController {

      public BidSummariesController(ApexPages.StandardController controller) {

    }
    
    public BidSummariesController(ApexPages.StandardSetController controller) {

    }

        public List<Bid_Summaries__c> Bid {get; set;}
        public BidSummariesController(){
        List<Bid_Summaries__c> Bid= [Select Id, Selected_Option__c,Concession__c, Status__c,TJH_Service_Fee__c,
                                    Upgrade_Amount__c,Upgrade_Total__c, Original_Budget__c From Bid_Summaries__c] ;
    }
                       PageReference pageRef = Page.BidSummary1;                    
                       public list <Bid_Summaries__c> getBid(){
                                              
                       return Bid;
                       
        }
    
    }

Visualforce page:

<apex:page standardController="Bid_Summaries__c"> <apex:pageBlock title="Bid Summary"> <apex:slds /> <style type="text/css"> body {background-color: white;} p { font-weight: bold; } p { font-style: italic;} h1 { color: #f00; } p { background-color: #eec; } newLink { color: #f60; font-weight: bold; } </style> <apex:page lightningStylesheets="true"/> <apex:form > <apex:pageBlockTable value="{! Bid_Summaries__c }" var="Bid_Summaries__c"> <apex:column value="{! Bid_Summaries__c.Name }"/> <apex:column value="{! Bid_Summaries__c.Original_Budget__c }"/> <apex:column value="{! Bid_Summaries__c.Upgrade_Amount__c }"/> <apex:column value="{! Bid_Summaries__c.Budget_incl_Upgrades__c }"/> <apex:column value="{! Bid_Summaries__c.TJH_Service_Fee__c }"/> <apex:column value="{! Bid_Summaries__c.Concession__c }"/> <apex:column value="{! Bid_Summaries__c.Upgrade_Total__c}"/> </apex:pageBlockTable> </apex:form> </apex:pageBlock> </apex:page>
 
Salah Odeh 10Salah Odeh 10
Update visualforce page:

<apex:page standardController="Bid_Summaries__c" recordSetVar="Bid_Summaries__c" extensions="BidSummariesController"> <apex:pageBlock title="Bid Summary"> <apex:slds /> <style type="text/css"> body {background-color: white;} p { font-weight: bold; } p { font-style: italic;} h1 { color: #f00; } p { background-color: #eec; } newLink { color: #f60; font-weight: bold; } </style> <apex:page lightningStylesheets="true"/> <apex:form > <apex:pageBlockTable value="{! Bid_Summaries__c }" var="b"> <apex:column value="{! b.Name }"/> <apex:column value="{! b.Original_Budget__c }"/> <apex:column value="{! b.Upgrade_Amount__c }"/> <apex:column value="{! b.Budget_incl_Upgrades__c }"/> <apex:column value="{! b.TJH_Service_Fee__c }"/> <apex:column value="{! b.Concession__c }"/> <apex:column value="{! b.Upgrade_Total__c}"/> </apex:pageBlockTable> </apex:form> </apex:pageBlock> </apex:page>
shaik murthujavalishaik murthujavali
Hi,
Try this below test class once.
 
@isTest
public class BidSummariesControllertest {
    @isTest
    private static void testitnow(){
        list<Bid_Summaries__c> bids= new list<Bid_Summaries__c>();
        Bid_Summaries__c b = new Bid_Summaries__c();
            b.Selected_Option__c   = true;
             b.Upgrade_Amount__c     = 1000;
             b.Concession__c         = 1000;         
             b.Original_Budget__c = 100;
             b.Status__c  = 'New';
        bids.add(b);
        insert bids;
        BidSummariesController c = new BidSummariesController ();
        c.Bid = acclist;
        c.getBid();
        /* as you are not getting any records in controller they might not cover.
         * if you use any record in that pass them in test class like below.
         ApexPages.StandardController sc = new ApexPages.StandardController(recordId);
        BidSummariesController controller = new BidSummariesController (sc);
*/
    }
}

Hope it helps.

Thanks,
Shaik Murthujavali
Salah Odeh 10Salah Odeh 10
Thanks, I'm getting the following errors:

Variable does not exist acclist
Variable does not exist recordId
shaik murthujavalishaik murthujavali
Hi ,
I used different names here.
  1. Pass Listofbitsummaries in place of acclist.
  2. In your class you are not getting/using any recordid so you can't pass record id here.Leave it for now.
Thanks,
Shaik Murthujavali.
tuaroivao nhamnhientuaroivao nhamnhien

Update visualforce page:

<apex:page standardController="Bid_Summaries__c" recordSetVar="Bid_Summaries__c" extensions="BidSummariesController"> <apex:pageBlock title="Bid Summary"> <apex:slds /> <style type="text/css"> body {background-color: white;} p { font-weight: bold; } p { font-style: italic;} h1 { color: #f00; } p { background-color: #eec; } newLink { color: #f60; font-weight: bold; } </style> <apex:page lightningStylesheets="true"/> <apex:form > <apex:pageBlockTable value="{! Bid_Summaries__c }" var="b"> <apex:column value="{! b.Name }"/> <apex:column value="{! b.Original_Budget__c }"/> <apex:column value="{! b.Upgrade_Amount__c }"/> <apex:column value="{! b.Budget_incl_Upgrades__c }"/> <apex:column value="{! b.TJH_Service_Fee__c }"/> <apex:column value="{! b.Concession__c }"/> <apex:column value="{! b.Upgrade_Total__c}"/> </apex:pageBlockTable> </apex:form> </apex:pageBlock> </apex:page>
---------------

iPrice.vn (https://iprice.vn/dien-thoai-may-tinh-bang/dien-thoai-thong-minh/)