1

Can I get some help with my test class for my visualforce page? The page lists contacts and then the controller creates an activity for each contact selected after a button is clicked.

I am receiving the following error for my test class:

Error: Compile Error: Constructor not defined: [AttendanceController].<Constructor>(ApexPages.StandardController) at line 19 column 30

(I highlighted line 19 in the test class with double asterisks.)

My VisualForce Page:

<apex:page standardController="Contact" extensions="AttendanceController" recordSetVar="contacts" action="{!trackAttendance}">
   <apex:form >
       <apex:pageBlock title="Track Attendance">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!logAttendance}" value="Confirm"/>
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!selected}" var="contact">
                <apex:column value="{!contact.Name}"/>
                <apex:column value="{!contact.Account.Name}"/>
                <apex:column value="{!contact.Date_Last_Attended__c}"/>
            </apex:pageBlockTable> 
        </apex:pageBlock>
    </apex:form>    
</apex:page>

My controller class:

public with sharing class AttendanceController 
{
    private ApexPages.StandardSetController standardController;

public AttendanceController(ApexPages.StandardSetController standardController)
{
    this.standardController = standardController;
}


public PageReference trackAttendance()
{       
    // Get the selected records (optional, you can use getSelected to obtain ID's and do your own SOQL)
    List<Contact> selectedPeoples = (List<Contact>) standardController.getSelected();
    List<Task> tasksToInsert = new List<Task>();

    Date Sunday = Date.today().toStartofWeek();


    System.debug('@@@@@@@@@@@');


    // Update records       
    for(Contact selectedPeople : selectedPeoples)
    {
       //try {
           //Insert Attendance Record
           //RecordType rt = [SELECT Id FROM RecordType WHERE Name = 'Some Record Type' AND SobjectType = 'Task'];
           Task logAttendance = new Task(OwnerID=UserInfo.getUserId(), Type = 'Sunday Morning Attendance', Subject = 'Sunday Morning Attendance', Description = '', Status='Completed', Priority='Normal', ActivityDate=Sunday);
           logAttendance.WhoID = selectedPeople.Id;
           tasksToInsert.add(logAttendance);
           //Update Last Attendance Date Field on Contact Record
           selectedPeople.Date_Last_Attended__c = Sunday;
       // } catch (DmlException e) {
       //     System.debug('The following exception has occurred: ' + e.getMessage());
       // }
    }       

    insert tasksToInsert;
    return null;        
}

public PageReference logAttendance()
{       
    // Call StandardSetController 'save' method to update (optional, you can use your own DML)
    return standardController.save();   
    }
}

My Test Class:

@isTest
private class trackAttendanceTest {

    static testMethod void trackAttendanceTest() {

    //Insert your mock data here
    contact newContact= new contact();
    newContact.lastName='contact1';
    insert newContact;
    contact newContact1= [select id,name from contact where lastName= 'contact1'];

    //Construct your pagereference
    PageReference testPage = new pagereference('/apex/attendanceTracker');
    testPage.getParameters().put('id', 'newContact1.id');

    Test.setCurrentPage(testPage);

    ApexPages.StandardController sc = new ApexPages.standardController(newContact1);
    **AttendanceController a = new AttendanceController(sc);**

    Test.startTest();
    PageReference track = a.trackAttendance();
    PageReference log = a.logAttendance();
    Test.stopTest();

    //Adjust these asserts for your code.
    System.assertEquals('expectedUrl',pageRef.getUrl());
    Map<String,String> parms = pageRef.getParameters();
    //System.assertEquals(expectedNumParameters,pageRef.values().size());
    //System.assertEquals('expectedVal',parms.get('paramkey));


    }
}

2 Answers 2

4

Your test class method should look like this:

static testMethod void trackAttendanceTest() {

//Insert your mock data here
contact newContact= new contact();
newContact.lastName='contact1';
insert newContact;
contact[] newContact1 = [select id,name from contact];

//Construct your pagereference
PageReference testPage = new pagereference('/apex/attendanceTracker');

Test.setCurrentPage(testPage);

ApexPages.StandardSetController sc = new ApexPages.StandardSetController(newContact1);
AttendanceController a = new AttendanceController(sc);

Test.startTest();
PageReference track = a.trackAttendance();
PageReference log = a.logAttendance();
Test.stopTest();

//Adjust these asserts for your code.
System.assertEquals('expectedUrl',pageRef.getUrl());
Map<String,String> parms = pageRef.getParameters();
//System.assertEquals(expectedNumParameters,pageRef.values().size());
//System.assertEquals('expectedVal',parms.get('paramkey));


}

Notice that newContact1 needs to be a List instead of single contact variable, and these two lines as well:

ApexPages.StandardSetController sc = new ApexPages.standardSetController(newContact1);
AttendanceController a = new AttendanceController(sc);
3
  • @John_Yankee why did u edited an accepted answer with new incorrect code? You have actually made the testMethod throw error when saving the class, since StandardController accepts a single records, while StandardSetController can accept multiple sobject records.
    – VarunC
    Oct 11, 2016 at 7:09
  • in my testing, only StandardController would work whereas StandardSetController was throwing an error.
    – John_Henry
    Oct 11, 2016 at 16:45
  • Then there would be some other error in your code. The StandardController is a single record controller whereas StandardSetController is a list controller and standard list operations are assecible from this which was the usecase of original question. Setting a recordSetVar attribute in page component automatically makes StandardSetController requirement.
    – VarunC
    Oct 11, 2016 at 17:03
1

Your constructor is defined as taking a standard set controller, not a standard controller

public AttendanceController(ApexPages.StandardSetController standardController)
{
this.standardController = standardController;
}

Your variable is confusingly name "StandardController, even though that is not what it is. Below, you are attempting to pass a standard controller.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .