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
Alex KirbyAlex Kirby 

StandardSetController test class help?

Hi all, I am really struggling with creating a testclass for the below:
 
public with sharing class myCampaigns {

	public string selectedValue { get;set; }
    public List<SelectOption> statusOptions { get;set; }
    Public Integer noOfRecords{get; set;}
    Public Integer size{get;set;}    
    public string inputText1{get;set;}
            
    string queryString;
    id uId;  
    public ApexPages.StandardSetController setCon {
    
        get{
            if(setCon == null){ 
                uId = UserInfo.getUserID();
                size = 30;
                if(selectedValue == 'All' || selectedValue == null){
                queryString = 'Select id, Company, Name, Campaign_Code__c, Ownerid, LeadSource, Status, Opportunity_Start_Date__c, Opportunity_End_Date__c, Opportunity_Close_Date__c, Opportunity_Type__c From Lead where OwnerId = :uId';    
                }else{
                queryString = 'Select id, Company, Name, Campaign_Code__c, Ownerid, LeadSource, Status, Opportunity_Start_Date__c, Opportunity_End_Date__c, Opportunity_Close_Date__c, Opportunity_Type__c From Lead where OwnerId = :uId and campaign_code__c =: selectedValue';
                }
                setCon = new ApexPages.StandardSetController(Database.getQueryLocator(queryString));
                setCon.setPageSize(size);
                noOfRecords = setCon.getResultSize();
            }
            return setCon;
           
        }set;  
    }
   
    Public List<Lead> getLeads(){
        List<Lead> leadList = new List<Lead>();
        for(Lead l : (List<Lead>)setCon.getRecords())
            leadList.add(L);
        return leadList;
    }
     
    public pageReference refresh() {
        setCon = null;
        getleads();
        setCon.setPageNumber(1);
        return null;
    }
     
    public Boolean hasNext {
        get {
            return setCon.getHasNext();
        }
        set;
    }
    public Boolean hasPrevious {
        get {
            return setCon.getHasPrevious();
        }
        set;
    }
  
    public Integer pageNumber {
        get {
            return setCon.getPageNumber();
        }
        set;
    }
  
    public void first() {
        setCon.first();
    }
  
    public void last() {
        setCon.last();
    }
  
    public void previous() {
        setCon.previous();
    }
  
    public void next() {
        setCon.next();
    }

    public void autoRun()
    {
        Schema.DescribeFieldResult statusFieldDescription = Lead.Campaign_code__c.getDescribe();
        statusOptions = new list<SelectOption>();
        
        for (Schema.Picklistentry picklistEntry : statusFieldDescription.getPicklistValues())
        {
            statusOptions.add(new SelectOption(pickListEntry.getValue(),pickListEntry.getLabel()));
        }
    }
}

I don't know why, I have tried many variations and still can't even get it to initialize the controller, just remains at 0%..
This is what I have so far and would of thought it would of at least shown something?
@isTest
public class test_myCampaigns {
    
    static testMethod void myCampaignsTest(){
        
        Lead l = new lead();
        l.firstName = 'Test';
        l.lastname = 'User';
        l.Company = 'Test Company';
        insert l;
        
        List <Lead> ld = [Select id from Lead];
        
        Test.startTest();
        
        Test.setCurrentPage(Page.myCampaigns);
        
        ApexPages.StandardSetController con = new ApexPages.StandardSetController(ld);
        myCampaigns ext = new myCampaigns(con);
        ext.getLeads();
        Test.stopTest();
        
    }
}

Help is always appreciated.
Best Answer chosen by Alex Kirby
JeffreyStevensJeffreyStevens
Oh - I don't think you're calling the autorun() method from the test method.  Try adding a call to that.

ext.autoRun();
 

All Answers

JeffreyStevensJeffreyStevens
Try putting a constructor in the controller class. 

public myCampaigns() {
}
Alex KirbyAlex Kirby
Sorry I don't understand, I have been checking forums and googling checking my code against blogs and I can't understand where I am going wrong.. Would you be able to elaborate on the above please?
Alex KirbyAlex Kirby
This is the error I am seeing, didn't realise I hadn't added it:

Constructor not defined: [myCampaigns].<Constructor>(ApexPages.StandardSetController)
JeffreyStevensJeffreyStevens
Ya - I didn't know for sure that missing the constructor would be a problem - but I was thinking it might.

so after line#10 - just add this


public myCampaigns() {
}


the constructor is a method that doesn't return anything, that get's executed when the class is instanstated.

 
Alex KirbyAlex Kirby
Ahh, I added the line previously and it didn't fire.
JeffreyStevensJeffreyStevens
Oh - I don't think you're calling the autorun() method from the test method.  Try adding a call to that.

ext.autoRun();
 
This was selected as the best answer
Alex KirbyAlex Kirby
d'oh, thanks. It is i've got 55% I can sort the rest. 

Really appreciate your help on this!