2

I am trying to write a test class for a StandardSetController with a very simple extension that processes a list of records that were selected from a related list. I am coming up short on how to get this to work and would love your input.

Visualforce Page: [JobReporting_MassReject.vfp]

<apex:page standardController="TR1__Job_Reporting__c" showHeader="false" sidebar="false" recordSetVar="records" extensions="JobReporting_RejectExt">
    <apex:form >    
        <apex:messages />
        <apex:pageBlock >
            <apex:pageMessages id="JobReportingRejectMessages"/>
            <apex:pageBlockTable value="{!selected}" var="jr">
                <apex:column value="{!jr.TR1__Contact__c}"/>
                <apex:column headerValue="Reject?" value="{!jr.TR1__isRejected__c}"/>
                <apex:column headerValue="Rejection Reason">
                    <apex:inputField value="{!jr.TR1__Rejection_Notification__c}" required="true"/>
                </apex:column>
                <apex:column headerValue="Rejection Notes">
                    <apex:inputField value="{!jr.TR1__Rejection_Notes__c}" style="width: 400px; height: 100px"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >            
                <apex:commandButton value="Confirm" action="{!save}"/>
                <apex:commandbutton value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>    
</apex:page>

Extension Class:[JobReporting_RejectExt.apxc]

public with sharing class JobReporting_RejectExt{
    ApexPages.StandardSetController setCon;
    public JobReporting_RejectExt(ApexPages.StandardSetController controller){
        setCon = controller;
        FOR (TR1__Job_Reporting__c jr:(TR1__Job_Reporting__c[])setCon.getSelected()){
            jr.TR1__isRejected__c = 'Yes';
        }
    }
}

Test Class:[JobReporting_RejectExt_Test.apxc]

@isTest
private class JobReporting_RejectExt_Test {
    public static testMethod void TestReject(){

        TR1__Job_Reporting__c jrTest = new TR1__Job_Reporting__c();

        PageReference pageRef = Page.JobReporting_MassReject;

        **ApexPages.StandardSetController stdJR = new ApexPages.StandardSetController(jrTest);**
        JobReporting_RejectExt jr_ext = new JobReporting_RejectExt(stdJR);

        System.assertEquals(jrTest.TR1__isRejected__c,'Yes');
    }
}

Error message (Line 9 of Test Class):

"Constructor not defined: [ApexPages.StandardSetController].(TR1__Job_Reporting__c)"

1 Answer 1

3

The StandardSetController class has two constructors, one accepts a list of sobjects and the other accepts a query locator. There is no constructor which accepts just a single record which is why you are getting the error above.

TR1__Job_Reporting__c jrTest = new TR1__Job_Reporting__c();

// create a list
List<TR1__Job_Reporting__c> yourList = new List<TR1__Job_Reporting__c>();
// add your record(s) to this list
yourList.add(jrTest);

PageReference pageRef = Page.JobReporting_MassReject;

// change the constructor parameter to the list of objects
ApexPages.StandardSetController stdJR = new ApexPages.StandardSetController(yourList);
1
  • Thank you, Mark! This is exactly what I needed to get this test class to work. Aug 16, 2017 at 22:52

You must log in to answer this question.

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