You need to sign in to do that
Don't have an account?
Writing a Controller extension test class
I am trying to create a test class for a controller extenstion that performs a SOQL query and sets up the VF page for pagination. Most test class examples that I have found have been for insertion of records, not data selection and listing. Can someone provide suggestions on the controller extension shown below? Thanks.
public class DealList{
public Integer noOfRecords{get; set;}
public Integer size{get;set;}
public boolean showHeaderSideBar{get;set;}
public String redirectUrl {get;set;}
ApexPages.StandardController controller;
private boolean fullscreen;
public DealList(ApexPages.StandardController controller) {
this.controller = controller;
//check if page needs to be opened in full screen mode
String value = ApexPages.currentPage().getParameters().get('fullscreen');
if(String.isNotBlank(value) && value == 'true'){
fullscreen = true;
showHeaderSideBar = true; }else{
fullscreen = false;
showHeaderSideBar = false;
}
}
public ApexPages.StandardSetController deals{
get{
if(deals == null){
if(fullscreen) size = 100;
if(size==null) size=5;
deals = new ApexPages.StandardSetController(Database.getQueryLocator( [Select Name, Location__c, Asset_Type__c, Underwriter__c, Amount__c, Stage__c, Estimated_Close__c, Closing_Probability_Category__c
from Deal__c
where Account_Name__c = :controller.getId()
and Status__c = 'Open']));
deals.setPageSize(size);
noOfRecords = deals.getResultSize();
}
return deals;
}
set;
}
public Boolean hasNext {
get {
return deals.getHasNext();
}
set;
}
public Boolean hasPrevious {
get {
return deals.getHasPrevious();
}
set;
}
public Integer pageNumber {
get {
return deals.getPageNumber();
}
set;
}
public void first() {
deals.first();
}
public void last() {
deals.last();
}
public void previous() {
deals.previous();
}
public void next() {
deals.next();
}
public PageReference refreshPageSize() {
deals.setPageSize(size);
return null;
}
public void showAll(){
redirectUrl = '/apex/AcctDealRelatedList?id='+controller.getId()+'&fullscreen=true';
}
public void saveDeals(){
try {
deals.save();
}
catch (Exception e) {
ApexPages.addMessages(e);
}
}
}
public class DealList{
public Integer noOfRecords{get; set;}
public Integer size{get;set;}
public boolean showHeaderSideBar{get;set;}
public String redirectUrl {get;set;}
ApexPages.StandardController controller;
private boolean fullscreen;
public DealList(ApexPages.StandardController controller) {
this.controller = controller;
//check if page needs to be opened in full screen mode
String value = ApexPages.currentPage().getParameters().get('fullscreen');
if(String.isNotBlank(value) && value == 'true'){
fullscreen = true;
showHeaderSideBar = true; }else{
fullscreen = false;
showHeaderSideBar = false;
}
}
public ApexPages.StandardSetController deals{
get{
if(deals == null){
if(fullscreen) size = 100;
if(size==null) size=5;
deals = new ApexPages.StandardSetController(Database.getQueryLocator( [Select Name, Location__c, Asset_Type__c, Underwriter__c, Amount__c, Stage__c, Estimated_Close__c, Closing_Probability_Category__c
from Deal__c
where Account_Name__c = :controller.getId()
and Status__c = 'Open']));
deals.setPageSize(size);
noOfRecords = deals.getResultSize();
}
return deals;
}
set;
}
public Boolean hasNext {
get {
return deals.getHasNext();
}
set;
}
public Boolean hasPrevious {
get {
return deals.getHasPrevious();
}
set;
}
public Integer pageNumber {
get {
return deals.getPageNumber();
}
set;
}
public void first() {
deals.first();
}
public void last() {
deals.last();
}
public void previous() {
deals.previous();
}
public void next() {
deals.next();
}
public PageReference refreshPageSize() {
deals.setPageSize(size);
return null;
}
public void showAll(){
redirectUrl = '/apex/AcctDealRelatedList?id='+controller.getId()+'&fullscreen=true';
}
public void saveDeals(){
try {
deals.save();
}
catch (Exception e) {
ApexPages.addMessages(e);
}
}
}
Try this once
All Answers
Method does not exist or incorrect signature: [DealList].hasNext() at line 48 column 35. Not sure why. Thanks
Try this once
// verify if 10 rows returned
46 system.assertEquals(10, stdController.noOfRecords);
47
48 system.assertEquals(true, stdController.hasNext);
49 system.assertEquals(false, stdController.hasPrevious);
50 system.assertEquals(1, stdController.pageNumber);
51
52 stdController.next();
53 system.assertEquals(2, stdController.pageNumber);
54 stdController.last();
55 system.assertEquals(2, stdController.pageNumber);
56 stdController.previous();
57 system.assertEquals(2, stdController.pageNumber);
58 stdController.first();
59 system.assertEquals(1, stdController.pageNumber);
I have removed all assertEquals() except one here.
Can you try this and let me know how much is the coverage?
The issue is that not all Accounts have Deals associated with them. This is a VF page that returns the list of Deals for an Account if they exist; if not, the list is blank which is what is expected. There may also only be 1 or 2 deals associated with an account, so the value of 10 as a test will likely fail against a randomly selected group of Accounts.
Can you put debug
system.debug('===No of record===='+stdController.noOfRecords);
before
system.assertEquals(10, stdController.noOfRecords);
line.
And check the output in debug logs.
where Account_Name__c = :controller.getId()
and Status__c = 'Open'] query should return 10 records as per the test data.
System.AssertException: Assertion Failed: Expected: 10, Actual: null
I think I understood the mistake. Please try the above code now
ApexPages.StandardSetController setCon = stdController.deals;
I had missed it.
That made a big difference. 58% coverage now. The failure is now on line 56, col 1:"System.AssertException: Assertion Failed: Expected: 2, Actual: 1 "
Ln 56: system.assertEquals(2, stdController.pageNumber); - after stdController.next();
public PageReference refreshPageSize() {
deals.setPageSize(size);
return null;
}
public void showAll(){
redirectUrl = '/apex/AcctDealRelatedList?id='+controller.getId()+'&fullscreen=true';
}
public void saveDeals(){
try {
deals.save();
} catch (Exception e) {
ApexPages.addMessages(e);
After stdController.first() and before // turn of fullscreen
Add these lines:
stdController.refreshPageSize();
stdController.showAll();
stdController.saveDeals();
} catch (Exception e) {
ApexPages.addMessages(e);
Create another test method in the same class
I would recommend you to go through trialhead modules for further learning.
Its midnight here! Sleepy. Bye Robert. Happy coding
I am a trailhead believer myself. 44 badges so far; however, nothing beats the real life example.
Thank you again for the real life lesson. Good night.