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
asd@as.asdasd@as.asd 

Test class

hello,

    i am facing a problem to write a test class of  a apex class.

   my test class cover 90% part of apex class.

   Apex class 

 

public class AccountSearchPagination {


public List<Account> accList { get; set; }

public String selectedCity { get; set; }
public String selectedState { get; set; }
public String selectedCountry { get; set; }

public List<SelectOption> cities { get; set; }
public List<SelectOption> states { get; set; }
public List<SelectOption> countries { get; set; }

public ApexPages.Standardsetcontroller setCon;

public AccountSearchPagination(){
cities = new List<SelectOption>();
states = new List<SelectOption>();
countries = new List<SelectOption>();

cities.add(new SelectOption('','-Select City-'));
states.add(new SelectOption('','-Select State-'));
countries.add(new SelectOption('','-Select Country-'));

selectedCity = '';
selectedState = '';
selectedCountry = '';

for(Account a : [Select id,name,BillingCity,BillingState,BillingCountry from Account]){
if(a.BillingCity!=null && a.BillingCity!=''){
cities.add(new SelectOption(a.BillingCity,a.BillingCity));
}
if(a.BillingState!=null && a.BillingState!=''){
states.add(new SelectOption(a.BillingState,a.BillingState));
}
if(a.BillingCountry!=null && a.BillingCountry!='' ){
countries.add(new SelectOption(a.BillingCountry,a.BillingCountry));
}
system.debug('This is provided in for loop of aaccount'+accList);
}
system.debug('This is provided in constructor'+accList);

setCon = new Apexpages.Standardsetcontroller(Database.getQueryLocator(generateQuery()));
setCon.setPageSize(0);
}

public String generateQuery(){
String query = 'select id,name from Account';
String whereClause = '';

if(selectedCity!=null && selectedCity!=''){
whereClause =' BillingCity =\''+selectedCity+'\'';
}
if(selectedState!=null && selectedState!=''){
if(whereClause!=''){
whereClause +=' and ';
}
whereClause +=' BillingState =\'' + selectedState + '\'';
}
if(selectedCountry!=null && selectedCountry!=''){
if(whereClause!=''){
whereClause +=' and ';
}
whereClause +=' BillingCountry =\''+ selectedCountry +'\'';
}
if(whereClause!=''){
query +=' Where '+whereClause;
}
return query;
}

public List<Account> getSearchAccountDetails(){
return (List<Account>) setCon.getRecords();
}

public PageReference search() {
setCon = new Apexpages.Standardsetcontroller(Database.getQueryLocator(generateQuery()));
setCon.setPageSize(10);
return null;
}

public Pagereference next(){
setCon.next();
return null;
}
public Pagereference last(){
setCon.last();
return null;
}
public Pagereference previous(){
setCon.previous();
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 {setCon.setPageNumber(1);}
}

}

 

now how to test code in red colour.

Chris JohnChris John

The docs for StandardSetController:

http://www.salesforce.com/us/developer/docs/pages/Content/apex_pages_standardsetcontroller.htm

 

You can use setSelected(sObjects[] selectedRecords) to assign test records to it, then your test can validate the records returned by next, previous, last etc.

Navatar_DbSupNavatar_DbSup

Hi,
You have to simply call those methods with instance of that class like this:
AccountSearchPagination c=new AccountSearchPagination();
c.next();
c.last();
c.previous();