You need to sign in to do that
Don't have an account?
Salah 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>();
}
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>();
}
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
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)
@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>
<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>
Try this below test class once.
Hope it helps.
Thanks,
Shaik Murthujavali
Variable does not exist acclist
Variable does not exist recordId
I used different names here.
- Pass Listofbitsummaries in place of acclist.
- 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.
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/)