You need to sign in to do that
Don't have an account?
subodh chaturvedi 17
How to write the test class of visual force Controller Extension
This is my Class whose test Class i have written But is failing everytime.with error First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Owner ID: owner cannot be blank: [OwnerId] .
public with sharing class CU_Detail_Growth {
Public id accRecId {get;set;}
public list<Account> accList{get;set;}
Public Integer size{get;set;}
Public Integer noOfRecords{get; set;}
/* public CU_Detail_Growth (){
}*/
/* public CU_Detail_Growth(ApexPages.StandardController controller) {
}*/
public CU_Detail_Growth(ApexPages.StandardsetController controller) {
accRecId = [select id,Account_Executive__c from Territory_Plan__c where id = :ApexPages.currentPage().getParameters().get('id')].Account_Executive__c;
system.debug('****'+accRecId);
}
public ApexPages.StandardSetController setCon {
get{
if(setCon == null){
size = 5;
string queryString = 'select id,OwnerId,Name,Account_Category__c,of_Members__c,Market_Segment__c,Account_Status__c,Action_Plan__c,Relationship_Assessment__c FROM Account Where Account_Category__c =\'Growth\' AND OwnerId =:accRecId' ;
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
setCon.setPageSize(size);
noOfRecords = setCon.getResultSize();
}
return setCon;
}set;
}
Public List<Account> getAccounts(){
List<Account> accList = new List<Account>();
for(Account a : (List<Account>)setCon.getRecords())
accList.add(a);
return accList;
}
public pageReference refresh() {
setCon = null;
getAccounts();
setCon.setPageNumber(1);
return null;
}
public Boolean hasNext {
get {
return setCon.getHasNext();
}
set;
}
public Boolean hasPrevious {
get {
return setCon.getHasPrevious();
}
set;
}
public Integer pageNumber {
get {
return setCon.getPageNumber();
}
set;
}
public void first() {
setCon.first();
}
public void last() {
setCon.last();
}
public void previous() {
setCon.previous();
}
public void next() {
setCon.next();
}
}
Test Class:-
@IsTest
Public class TestCU_Detail_Growth {
Public static testMethod Void detailGrowth(){
User u = new User(
ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
LastName = 'last',
Email = 'puser000@amamama.com',
Username = 'puser000@amamama.com' + System.currentTimeMillis(),
CompanyName = 'TEST',
Title = 'title',
Alias = 'alias',
TimeZoneSidKey = 'America/Los_Angeles',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_US'
);
Account acc =new Account(Name='1st Advantage CU ',Account_Category__c ='Growth',OwnerId=u.id);
Insert acc;
List<Territory_Plan__c> t = new List<Territory_Plan__c>();
Territory_Plan__c tps = new Territory_Plan__c();
tps.Account_Executive__c=u.id;
tps.of_Accounts__c=14;
tps.Fiscal_Year__c='2018';
tps.Total_Years_Membership__c=120;
tps.of_Accounts_with_Bill_Pay__c=12;
tps.of_Accounts_with_Credit__c=14;
tps.of_Accounts_with_Debit__c=5;
// tps.Account__c=acc.id;
tps.of_Accounts_with_TMC__c=7;
t.add(tps);
Insert t;
Test.StartTest();
PageReference pageRef = Page.CU_Growth;
pageRef.getParameters().put('id', String.valueOf(acc.Id));
Test.setCurrentPage(Page.CU_Growth);
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(t);
stdSetController.setSelected(t);
CU_Detail_Growth ext = new CU_Detail_Growth (stdSetController);
ext.getAccounts();
ext.refresh();
ext.first();
ext.last();
ext.previous();
ext.next();
Test.StopTest();
}
}
public with sharing class CU_Detail_Growth {
Public id accRecId {get;set;}
public list<Account> accList{get;set;}
Public Integer size{get;set;}
Public Integer noOfRecords{get; set;}
/* public CU_Detail_Growth (){
}*/
/* public CU_Detail_Growth(ApexPages.StandardController controller) {
}*/
public CU_Detail_Growth(ApexPages.StandardsetController controller) {
accRecId = [select id,Account_Executive__c from Territory_Plan__c where id = :ApexPages.currentPage().getParameters().get('id')].Account_Executive__c;
system.debug('****'+accRecId);
}
public ApexPages.StandardSetController setCon {
get{
if(setCon == null){
size = 5;
string queryString = 'select id,OwnerId,Name,Account_Category__c,of_Members__c,Market_Segment__c,Account_Status__c,Action_Plan__c,Relationship_Assessment__c FROM Account Where Account_Category__c =\'Growth\' AND OwnerId =:accRecId' ;
setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
setCon.setPageSize(size);
noOfRecords = setCon.getResultSize();
}
return setCon;
}set;
}
Public List<Account> getAccounts(){
List<Account> accList = new List<Account>();
for(Account a : (List<Account>)setCon.getRecords())
accList.add(a);
return accList;
}
public pageReference refresh() {
setCon = null;
getAccounts();
setCon.setPageNumber(1);
return null;
}
public Boolean hasNext {
get {
return setCon.getHasNext();
}
set;
}
public Boolean hasPrevious {
get {
return setCon.getHasPrevious();
}
set;
}
public Integer pageNumber {
get {
return setCon.getPageNumber();
}
set;
}
public void first() {
setCon.first();
}
public void last() {
setCon.last();
}
public void previous() {
setCon.previous();
}
public void next() {
setCon.next();
}
}
Test Class:-
@IsTest
Public class TestCU_Detail_Growth {
Public static testMethod Void detailGrowth(){
User u = new User(
ProfileId = [SELECT Id FROM Profile WHERE Name = 'System Administrator'].Id,
LastName = 'last',
Email = 'puser000@amamama.com',
Username = 'puser000@amamama.com' + System.currentTimeMillis(),
CompanyName = 'TEST',
Title = 'title',
Alias = 'alias',
TimeZoneSidKey = 'America/Los_Angeles',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US',
LocaleSidKey = 'en_US'
);
Account acc =new Account(Name='1st Advantage CU ',Account_Category__c ='Growth',OwnerId=u.id);
Insert acc;
List<Territory_Plan__c> t = new List<Territory_Plan__c>();
Territory_Plan__c tps = new Territory_Plan__c();
tps.Account_Executive__c=u.id;
tps.of_Accounts__c=14;
tps.Fiscal_Year__c='2018';
tps.Total_Years_Membership__c=120;
tps.of_Accounts_with_Bill_Pay__c=12;
tps.of_Accounts_with_Credit__c=14;
tps.of_Accounts_with_Debit__c=5;
// tps.Account__c=acc.id;
tps.of_Accounts_with_TMC__c=7;
t.add(tps);
Insert t;
Test.StartTest();
PageReference pageRef = Page.CU_Growth;
pageRef.getParameters().put('id', String.valueOf(acc.Id));
Test.setCurrentPage(Page.CU_Growth);
ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(t);
stdSetController.setSelected(t);
CU_Detail_Growth ext = new CU_Detail_Growth (stdSetController);
ext.getAccounts();
ext.refresh();
ext.first();
ext.last();
ext.previous();
ext.next();
Test.StopTest();
}
}
Best Answer chosen by subodh chaturvedi 17
Steven Nsubuga