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
SayasoniSayasoni 

Help with Test class

Am trying to write a test class for the following controller but i keep on getting this error: Error: Compile Error: Constructor not defined: [ListContactsController].<Constructor>(ApexPages.StandardSetController) at line 33 column 39

 

Below is my controller & the test class:

 

public class ListContactsController {

public List<Contact> contacts;

public ApexPages.StandardSetController setCtrl

    {
        get {
            if(setCtrl == null) {
                setCtrl = new ApexPages.StandardSetController(Database.getQueryLocator([Select Id, LastName,Email FROM Contact Order By LastName]));
                // sets the number of records in each page set
                setCtrl.setPageSize(10);
            }
            return setCtrl;
            
        }
     set;
    } 
    // indicates whether there are more records after the current page set.
    public Boolean hasNext {
        get {
            return setCtrl.getHasNext();
        }
        set;
    }
 
    // indicates whether there are more records before the current page set.
    public Boolean hasPrevious {
        get {
            return setCtrl.getHasPrevious();
        }
        set;
    }
 
    // returns the page number of the current page set
    public Integer pageNumber {
        get {
            return setCtrl.getPageNumber();
        }
        set;
    }
      
    // returns the first page of records
    public void first() {
        setCtrl.first();
    }
 
    // returns the last page of records
    public void last() {
        setCtrl.last();
    }
 
    // returns the previous page of records
    public void previous() {
        setCtrl.previous();
    }
 
    // returns the next page of records
    public void next() {
        setCtrl.next();
    }
 
    // returns the PageReference of the original page, if known, or the home page.
    public void cancel() {
        setCtrl.cancel();
    }
    
    public List<Contact> getContacts()
    {
        return (List<Contact>)setCtrl.getRecords();
    }
 
}

                                                

Test Class:

@IsTest
private class ListContactsTest{

static testMethod void testListContacts() {


 Contact con1 = new Contact(
                Lastname = 'test contact',
                Email = 'test@test.com');
         insert con1;
             
 Contact con2 = new Contact(
                Lastname = 'test contact2',
                Email = 'test@test2.com');
            insert con2;
            
 Contact con3 = new Contact(
                Lastname = 'test contact3',
                Email = 'test@test3.com');
        insert con3;
        
 List<Contact> cont = new List<Contact>();
 cont.add(con1);
 cont.add(con2);
 cont.add(con3);
 
 insert cont;
 
  PageReference pageRef = Page.listcontacts;
        Test.setCurrentPage(pageRef);
             
    ApexPages.StandardSetController ssc = new ApexPages.Standardsetcontroller(cont);
        ListContactsController ctrl = new ListContactsController(ssc); //line 33
        ssc.getHasNext();
        ssc.getHasPrevious();
        ssc.getPageNumber();
        ctrl.first();
        ctrl.getContacts();
        ctrl.last();
        ctrl.next();
        ctrl.previous();
    
  }
  }
 
 
 

 

 


Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet with 78% code coverage

@IsTest

private class ListContactsTest{

 

static testMethod void testListContacts() {

 

 

 Contact con1 = new Contact(

                Lastname = 'test contact',

                Email = 'test@test.com');

            

 Contact con2 = new Contact(

                Lastname = 'test contact2',

                Email = 'test@test2.com');

          

 Contact con3 = new Contact(

                Lastname = 'test contact3',

                Email = 'test@test3.com');

     

 List<Contact> cont = new List<Contact>();

 cont.add(con1);

 cont.add(con2);

 cont.add(con3);

 

 insert cont;

 

 

  PageReference pageRef = Page.listcontacts;

  Test.setCurrentPage(pageRef);

            

    ApexPages.StandardSetController ssc = new ApexPages.Standardsetcontroller(cont);

        ListContactsController ctrl = new ListContactsController(); //line 33

        ssc.getHasNext();

        ssc.getHasPrevious();

        ssc.getPageNumber();

        ctrl.first();

        ctrl.getContacts();

        ctrl.last();

        ctrl.next();

        ctrl.previous();

   

  }

  }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

Try the below code snippet with 78% code coverage

@IsTest

private class ListContactsTest{

 

static testMethod void testListContacts() {

 

 

 Contact con1 = new Contact(

                Lastname = 'test contact',

                Email = 'test@test.com');

            

 Contact con2 = new Contact(

                Lastname = 'test contact2',

                Email = 'test@test2.com');

          

 Contact con3 = new Contact(

                Lastname = 'test contact3',

                Email = 'test@test3.com');

     

 List<Contact> cont = new List<Contact>();

 cont.add(con1);

 cont.add(con2);

 cont.add(con3);

 

 insert cont;

 

 

  PageReference pageRef = Page.listcontacts;

  Test.setCurrentPage(pageRef);

            

    ApexPages.StandardSetController ssc = new ApexPages.Standardsetcontroller(cont);

        ListContactsController ctrl = new ListContactsController(); //line 33

        ssc.getHasNext();

        ssc.getHasPrevious();

        ssc.getPageNumber();

        ctrl.first();

        ctrl.getContacts();

        ctrl.last();

        ctrl.next();

        ctrl.previous();

   

  }

  }

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

This was selected as the best answer
Srikant JoshiSrikant Joshi

As the error suggest, you havent defined the constructor but yet somehow invoking it through the test class.

 

The constructor would have been something like,

 

public ListContactsController (ApexPages.StandardController stdController) {
        //some code here
}

 

Hope this helps.

joshisrik@gmail.com
Senior Technical Analyst|Schneider Electric.