+ Start a Discussion
vinayakamurthyvinayakamurthy 

test class

How could i write a test class for a controller with out  its constructor class??

 

example 1)public class convertToCLA {
List<Contact> contacts;
List<Lead> leads;
List<Account> accounts;

public void convertType(Integer phoneNumber) {
List<List<sObject>> results = [FIND '4155557000'
IN Phone FIELDS
RETURNING Contact(Id, Phone, FirstName, LastName),
Lead(Id, Phone, FirstName, LastName), Account(Id, Phone, Name)];
sObject[] records = ((List<sObject>)results[0]);

if (!records.isEmpty()) {
for (Integer i = 0; i < records.size(); i++) {
sObject record = records[i];
if (record.getSObjectType() == Contact.sObjectType) {
contacts.add((Contact) record);
} else if (record.getSObjectType() == Lead.sObjectType){
leads.add((Lead) record);
} else if (record.getSObjectType() == Account.sObjectType) {
accounts.add((Account) record);
}
}
}
}
}

 

 

2)public with sharing class CustomPaginationExt {
public List<Account> accounts{get;set;}
public Integer pageSize{get;set;}
public Integer noOfPages{get;set;}
public Integer pageNumber{get;set;}
private String baseQuery = 'select name, industry, annualRevenue from Account order by name';
private Integer totalNoOfRecs;

public CustomPaginationExt(ApexPages.StandardController controller) {
pageSize = 2;
totalNoOfRecs = [select count() from Account limit 50000];
getInitialAccountSet();
}

public PageReference getInitialAccountSet()
{
pageNumber = 0;
noOfPages = totalNoOfRecs/pageSize;

if (Math.mod(totalNoOfRecs, pageSize) > 0)
noOfPages++;

try{
accounts = Database.query(baseQuery + ' limit '+pageSize);
}
catch(Exception e){
ApexPages.addMessages(e);
}
return null;
}

public PageReference next(){
pageNumber++;

queryAccounts();
return null;
}
public PageReference previous(){
pageNumber--;
if (pageNumber < 0)
return null;

queryAccounts();
return null;
}

private void queryAccounts()
{
Integer offset = pageNumber * pageSize;
String query = baseQuery + ' limit '+pageSize +' offset '+ offset;
System.debug('Query is'+query);
try{
accounts = Database.query(query);
}
catch(Exception e){
ApexPages.addMessages(e);
}
}
}

Sonali BhardwajSonali Bhardwaj

There is always a default no parameterized constructor for a class if you not defined any other constructor yourself. You can directly instantiate your class and use it.

Sri549Sri549

Hi Vinay

This is the test class for  provided 2nd class 

@ IsTest
public class Test_CustomPaginationExt
 {
  public static testmethod void CustomPaginationExt()
  {    
      Account acc=new Account();
      acc.Name='test';
      Insert acc;
     
   ApexPages.StandardController Stdcon= new ApexPages.StandardController (acc);
       CustomPaginationExt cpe=new CustomPaginationExt(Stdcon);   
       cpe.getInitialAccountSet();
       cpe.next();
       cpe.previous();   
  } 
 }

 This test class provides you the suitable solution for your requirement please mark as soltuion and mark to Kudos if not please  ping me with some bit clear functionality. 

 

Thanks&Many Regards

Srinivas