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
Pavel BandarenkaPavel Bandarenka 

I am a newbie do not help me with the StandardSetController test.

Controller:public class AppointmentsViewController {


    public List<Doctor__c> doctorList {get;set;}
    public List<SelectOption> doctorSelectOptionList{get;set;}
    public List<Appointment__c> apointmentList{get;set;}
    public String doctorId {get;set;}
    public List<Patient__c> patientList{get;set;}
    public List<SelectOption> patientSelectOptionList{get;set;}
    public Integer noOfRecords{get; set;}
    public Integer size{get;set;}
    
    public AppointmentsViewController() {
       doctorList = [
           SELECT Id, Name, Working_Hours_Start__c, Working_Hours_End__c
           FROM Doctor__c
           ORDER BY Name ASC
       ];
        
        patientList = [
            SELECT Id, Name
            FROM Patient__c
        ];
        
        patientSelectOptionList = new List<SelectOption>();
        
        for(Patient__c item :patientList)
        {
            patientSelectOptionList.add(new SelectOption(item.Id, item.Name));
        }
        doctorId = doctorList.isEmpty() == FALSE ? doctorList[0].Id : NULL;
        
       doctorSelectOptionList = new List<SelectOption>();
        
        for(Doctor__c item :doctorList) {
            doctorSelectOptionList.add(new SelectOption(item.Id, item.Name));
        }
        getAppointments();
       
        
    }
    
    public void getAppointments () {
        apointmentList = [
            SELECT Id, Name, Doctor__c, Patient__c, Appointment_Date__c, Duration_in_minutes__c
            FROM Appointment__c
            WHERE Doctor__r.Id = :doctorId
        ];
    }



    public List<Appointment__c> Appointment { get{

        return (List<Appointment__c>)setCon.getRecords();

    } set; }



    public ApexPages.StandardSetController setCon {

        get{

            if(setCon == null){

                size = 20;

                List<Appointment__c> Appointment = [
                    SELECT Id, Name, Doctor__c, Patient__c, Appointment_Date__c, Duration_in_minutes__c
                    FROM Appointment__c];

                setCon = new ApexPages.StandardSetController(Appointment);

                setCon.setPageSize(size);

                noOfRecords = setCon.getResultSize();

            }

            return setCon;

        }set;

    }






}




Test:
@isTest
private class AppointmentsViewControllerTest {
    
    @isTest static void AppointmentsViewControllerTest1() {
        
        Doctor__c doctor = new Doctor__c(
        );
        
        insert doctor;
        
        AppointmentsViewController controller = new AppointmentsViewController();
        
        Appointment__c Appointment = new Appointment__c(
        );
        insert Appointment;
        
            ApexPages.standardSetController sc = new ApexPages.standardSetController(doctor

        
        
     // ApexPages.StandardSetController standardSetController = new ApexPages.StandardSetController(doctor);
        
    }
    
       
    

}
Best Answer chosen by Pavel Bandarenka
Maharajan CMaharajan C
Hi Pavel,

Sorry for the late reply.

Please use the belpw updated code:

t.setCon.getRecords(); ==> needs to be added
 
@isTest
public class AppointmentsViewControllerTest {
    @isTest static void AppointmentsViewControllerTest1() {
        Doctor__c doc = new Doctor__c(Name = 'pavel');
        insert doc;
        Patient__c pat = new Patient__c(Name = 'Test');
        insert pat;
        Appointment__c app = new Appointment__c(Doctor__c = doc.Id, Patient__c = pat.Id);
        insert app;
        
        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, OwnerId FROM Appointment__c LIMIT 5]));
        stdSetController.setSelected([SELECT Id, OwnerId FROM Appointment__c LIMIT 5]);
        
        Test.startTest();
        AppointmentsViewController t = new AppointmentsViewController();
        t.setCon.getRecords();
        Test.stopTest();
     }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Pavel,

Please try the below code :
 
@isTest
public class AppointmentsViewControllerTest {
    @isTest static void AppointmentsViewControllerTest1() {
        Doctor__c doc = new Doctor__c(Name = 'pavel');
        insert doc;
        Patient__c pat = new Patient__c(Name = 'Test');
        insert pat;
        Appointment__c app = new Appointment__c(Doctor__c = doc.Id, Patient__c = pat.Id);
        insert app;
        
        PageReference pageRef = Page.Your_VF_PageName;  // Give your VF Page Name
        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, OwnerId FROM Appointment__c LIMIT 5]));
        stdSetController.setSelected([SELECT Id, OwnerId FROM Appointment__c LIMIT 5]);
        
        Test.startTest();
        Test.setCurrentPage(pageRef);
        AppointmentsViewController t = new AppointmentsViewController();
        Test.stopTest();
     }
}

Thanks,
Maharajan.C
Pavel BandarenkaPavel Bandarenka
Hi Maharajan C
Everything worked, thank you very much, but the test coverage code is 63% and needs at least 75%. Can you tell me what needs to be done?
Maharajan CMaharajan C
Please post your vf page also...
Maharajan CMaharajan C
Hi Pavel,

Sorry for the late reply.

Please use the belpw updated code:

t.setCon.getRecords(); ==> needs to be added
 
@isTest
public class AppointmentsViewControllerTest {
    @isTest static void AppointmentsViewControllerTest1() {
        Doctor__c doc = new Doctor__c(Name = 'pavel');
        insert doc;
        Patient__c pat = new Patient__c(Name = 'Test');
        insert pat;
        Appointment__c app = new Appointment__c(Doctor__c = doc.Id, Patient__c = pat.Id);
        insert app;
        
        ApexPages.StandardSetController stdSetController = new ApexPages.StandardSetController(Database.getQueryLocator([SELECT Id, OwnerId FROM Appointment__c LIMIT 5]));
        stdSetController.setSelected([SELECT Id, OwnerId FROM Appointment__c LIMIT 5]);
        
        Test.startTest();
        AppointmentsViewController t = new AppointmentsViewController();
        t.setCon.getRecords();
        Test.stopTest();
     }
}

Thanks,
Maharajan.C
This was selected as the best answer
Pavel BandarenkaPavel Bandarenka
Thank you very much, I love you)))