I need to write test class for this method involving the generic method to retrieve Picklist options:

public static Map<String,String> retrievePicklistOptions(String objApiName, String fieldApiName) {
    Map<String,String> options = new Map<String,String>();
    SObject instance = (SObject) Type.forName('Schema.' + objApiName).newInstance();
    Schema.DescribeFieldResult fieldResult = instance.getSObjectType().getDescribe().fields.getMap().get(fieldApiName).getDescribe();
    List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
    for( Schema.PicklistEntry f : ple) {
        options.put(f.getLabel(), f.getValue());
    return options;

Any help appreciated. Thanks!!

  • 1
    What have you tried so far? Where are you stuck?
    – Derek F
    May 16, 2023 at 11:33
  • Hello , Does this post answer your query How do I write an Apex unit test? ? May 16, 2023 at 11:34
  • @DerekF Unable to start as I am not sure whether I would need to add the picklist values in the setup of the test class or if would I be able to use the values already in the object
    – Makc M
    May 16, 2023 at 11:46
  • Using an apex method to get picklist options in a lightning component is no longer necessary you can just use the getPicklistValues adapter
    – Henk3000
    May 16, 2023 at 14:34
  • @Henk3000 This is for a specific scenario and needed to be done like this, that is why I had to do this than getting the values directly in the LWC.
    – Makc M
    May 17, 2023 at 6:28

1 Answer 1


SObjects and their fields are available in unit tests (even if the test is being run as part of a deployment along with the SObject and/or fields).

So in the simplest case, this is just another instance of "you test it the same way you test anything else". Have your test method call retrievePicklistOptions() and pass in a hard-coded sobject and picklist field name.

This approach is, however, susceptible to differences between orgs (one org may have extra picklist values, another might have deactivated some values), and the assertions you make would basically be "magic values".

Using the Stub API to mock the call to that method (and a little bit of dependency injection) would be another approach, but that may not be possible. The documentation says that we can't mock "system types" but stops short of explaining what that entails.

You must log in to answer this question.

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