+ Start a Discussion
jkucera3jkucera3 

How do I set StandardSetController in a test?

There seems to be nothing in the Apex guides on how to test a set controller.  This method runs when a button is clicked on a custom object list view and processes the records selected from the list view.

 

How do I test it?  I can't figure out how to get any records "selected" in the StandardSetController.

 

 

public with sharing class replacerRunAllSelectedRulesController{

    ApexPages.StandardSetController setCon;
    private PageReference savePage;

    public replacerRunAllSelectedRulesController(ApexPages.StandardController controller) {
        //Not sure why this is needed...
    }//replacerRunAllSelectedRulesController

    public replacerRunAllSelectedRulesController(ApexPages.StandardSetController controller) {
        setCon=controller;
        List<Replacer__c> selectedRules=new List<Replacer__c>();
    }//replacerRunAllSelectedRulesController
    

    public PageReference runSelectedRules(){
        List<Replacer__c> selectedRules=new List<Replacer__c>();
        for(sObject s: setCon.getSelected()){
            Replacer__c r=(Replacer__c)s;
            selectedRules.add(r);
        }//for 1
        String objectName='';
        Boolean sendEmail=TRUE;
        
        if(selectedRules.size()==0){
        	ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 
        	'You must select at least one rule.  Please go back and select a rule by checking the checkbox and try again.'));        
        }else{
	        
	        Integer numJobsCantBeRunNow=replacerExecute.replacerQueueBatchJobs(objectName, selectedRules, sendEmail);
	        if(numJobsCantBeRunNow==0){
	            ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.CONFIRM,'Replacement has begun.  This can take ~10-30 minutes for each 10,000 records.  You will receive 1 email confirmation for each different object as all rules for one object are run together in one job.');
	            ApexPages.addMessage(myMsg);
	        }else{
	            //else throw an error that they need to select fewer rules to run right now
	            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, 'The selected rules cannot be run now as they would exceed your companies batch job limit of 5.  One job is created for each different object.  Please unselect ' + numJobsCantBeRunNow + ' object\'s rules and try again.  To view the batch queue, click Setup-->Monitoring-->Apex Jobs'));
	        }//if 2
        }//if 1
        
        return ApexPages.CurrentPage();
    }//runSelectedRules
    
}//replacerRunAllSelectedRulesController

 

Here's where I'm at - I have the controller assigned, but can't figure out how to select the records in the set controller:

 

 

public static testMethod void verifyListViewRunSelectedRulesButton() {
	Boolean deleteReplacers=TRUE;
        Boolean deleteQueuedJobs=FALSE;
        Boolean deleteFieldList=FALSE;
        Boolean deleteTestData=TRUE;
        Boolean deleteJobHistory=FALSE;
        Boolean abortBatchJobs=FALSE;
        //There should be zero replacers after this method runs
        deleteTestData(deleteReplacers, deleteQueuedJobs, deleteFieldList, deleteTestData, deleteJobHistory, abortBatchJobs);

        //Make 1 active rule so that a preview can be shown
        Boolean rActive=TRUE;
        String rObjectName='test__c';
        String rFieldName='picklist__c';
        String rFind='a';
        String rValue='b';
        List<replacer__c> rules=new List<replacer__c>();
        rules.add(createRule(rActive, rObjectName, rFieldName, rFind, rValue));
        rFieldName='text__c';
        rules.add(createRule(rActive, rObjectName, rFieldName, rFind, rValue));
        insert rules;

        Long numRecords=2;
        Date dat=date.today();
        DateTime datTim=dateTime.now();
        Decimal dec=0.123;
        String em='a@a.com';
        String ph='415-555-1234';
        String pick='a';            
        String text='a';
        String ur='http://www.google.com';

        //Insert some test data so we can play with the controller
        List<test__c> ts=createTestRecords(numRecords, dat, datTim, dec, em, ph, pick, text, ur); 
        pick='c';
        List<test__c> ts2=createTestRecords(1, dat, datTim, dec, em, ph, pick, text, ur);
        
        ApexPages.StandardSetController sc = new ApexPages.StandardSetController(rules);
        replacerRunAllSelectedRulesController scExt = new replacerRunAllSelectedRulesController(sc);
	//How do I select both "rules"?	
        test.StartTest();
            scExt.runSelectedRules();
        test.stopTest();

	List<test__c> results=[SELECT Id, picklist__c FROM test__c WHERE Id IN: ts];
        for(test__c t: results){
            system.assertEquals(rValue, t.picklist__c);
        }//for

        List<test__c> results2=[SELECT Id, picklist__c FROM test__c WHERE Id IN: ts2];
        for(test__c t: results2){
            system.assertEquals(pick, t.picklist__c);
        }//for
        
    }//verifyListViewRunSelectedRulesButton

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
David SchachDavid Schach

There is a method for standardSetController called "setSelected(sObject[] selectedrecords)" - use that in your test code:

 

sc.setSelected(rules);

 

That should help with one of them.  Do likewise to select all for the other.  Or copy just specific items from a List to a new one, and use those.

All Answers

David SchachDavid Schach

There is a method for standardSetController called "setSelected(sObject[] selectedrecords)" - use that in your test code:

 

sc.setSelected(rules);

 

That should help with one of them.  Do likewise to select all for the other.  Or copy just specific items from a List to a new one, and use those.

This was selected as the best answer
Brad BarrowesBrad Barrowes
David, that was awesome.  Thanks!

Brad