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
Yelper DevYelper Dev 

How to Write a Test class for Picklist Entry? Using Schema

Hello, can you please help me on how to write a test class method for the ff. snippet below. I used schema to have my picklsit be dynamic in visual force page.
 
public List<SelectOption> getAccountTypeOptionsList(){
        List<SelectOption> accountTypeOptionsList = new List<SelectOption>{new SelectOption('', '--None--')};
            Schema.DescribeFieldResult accountTypeDescription = Account.Type.getDescribe();
        List<Schema.PicklistEntry> accountTypePicklistValuesList = accountTypeDescription.getPicklistValues();
        for(Schema.PicklistEntry accountTypePicklistValue : accountTypePicklistValuesList){
            accountTypeOptionsList.add(new SelectOption(accountTypePicklistValue.getLabel(), accountTypePicklistValue.getValue()));
        }
        return accountTypeOptionsList;
    }

I need also the assertion. Thank you.
 
Best Answer chosen by Yelper Dev
Khan AnasKhan Anas (Salesforce Developers) 
Hi Yelper,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Controller:
public class PicklistEntryTest {
    
    public List<SelectOption> getAccountTypeOptionsList(){
        List<SelectOption> accountTypeOptionsList = new List<SelectOption>{new SelectOption('None', '--None--')};
            Schema.DescribeFieldResult accountTypeDescription = Account.Type.getDescribe();
        List<Schema.PicklistEntry> accountTypePicklistValuesList = accountTypeDescription.getPicklistValues();
        for(Schema.PicklistEntry accountTypePicklistValue : accountTypePicklistValuesList){
            accountTypeOptionsList.add(new SelectOption(accountTypePicklistValue.getLabel(), accountTypePicklistValue.getValue()));
        }
        return accountTypeOptionsList;
    }
}

Test Class:
@isTest
public class Test_PicklistEntryTest {

    static testMethod void testPicklist() {
        
        PicklistEntryTest pe = new PicklistEntryTest();

    	Test.startTest();
            List<SelectOption> options = pe.getAccountTypeOptionsList();
        Test.stopTest();
        
        system.assertEquals(options.get(0).getValue(), 'None');
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Yelper,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Controller:
public class PicklistEntryTest {
    
    public List<SelectOption> getAccountTypeOptionsList(){
        List<SelectOption> accountTypeOptionsList = new List<SelectOption>{new SelectOption('None', '--None--')};
            Schema.DescribeFieldResult accountTypeDescription = Account.Type.getDescribe();
        List<Schema.PicklistEntry> accountTypePicklistValuesList = accountTypeDescription.getPicklistValues();
        for(Schema.PicklistEntry accountTypePicklistValue : accountTypePicklistValuesList){
            accountTypeOptionsList.add(new SelectOption(accountTypePicklistValue.getLabel(), accountTypePicklistValue.getValue()));
        }
        return accountTypeOptionsList;
    }
}

Test Class:
@isTest
public class Test_PicklistEntryTest {

    static testMethod void testPicklist() {
        
        PicklistEntryTest pe = new PicklistEntryTest();

    	Test.startTest();
            List<SelectOption> options = pe.getAccountTypeOptionsList();
        Test.stopTest();
        
        system.assertEquals(options.get(0).getValue(), 'None');
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Yelper DevYelper Dev
Hello Khan,
 
@isTest static void picklistValueTest() {
        
        List<SelectOption> optionList = new List<SelectOption>();

    	Test.startTest();
        List<SelectOption> options = optionList.getAccountTypeOptionsList();
        Test.stopTest();
        
        system.assertEquals(options.get(0).getValue(), 'None');
    }
I replaced the class since it says invalid constructor Schema.Object. But I am still having an error inside the starttest() and stoptest(). My error says:

Error Message:
Method does not exist or incorrect signature: void getAccountTypeOptionsList() from the type List<System.SelectOption>

It won't recognize the class in my controller.
Khan AnasKhan Anas (Salesforce Developers) 
You need to create an instance of the class then call the method.
 
PicklistEntryTest pe = new PicklistEntryTest();   // create object of the class

List<SelectOption> options = pe.getAccountTypeOptionsList();    // call method through object

You need to use the correct class name.

Regards,
Khan Anas
Yelper DevYelper Dev
Hi Khan, it is now working. I forgot to create an object class using my custom controller. Thank you!!