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
Jesper Klang.Jesper Klang. 

Test Class for VisualEditor.DynamicPickList class

Hi,

I've tried for hours, but cannot seem to figure out how to create a test class for the Apex code below. I'm using it to present a dynamic picklist to a LWC based on values added to a Custom Metadata Type I created.

One error I'm facing when trying to call the class is "Method is not Visible". This is when I'm trying to call the class with a row first like this: 
brandedSectionColorPicklistValues bSCPV = new brandedSectionColorPicklistValues();
VisualEditor.DynamicPickListRows rows = bSCPV.getValues();
Here's the full class I'm trying to write a Test Class for:
global class brandedSectionColorPicklistValues extends VisualEditor.DynamicPickList {
    private VisualEditor.DynamicPickListRows  myValues = new VisualEditor.DynamicPickListRows();

    brandedSectionColorPicklistValues() {
        for (Brand_Color__mdt c : [SELECT MasterLabel, Background_Color__c, Font_Color__c FROM Brand_Color__mdt ORDER BY MasterLabel ASC]) {
            myValues.addRow(new VisualEditor.DataRow(c.MasterLabel, 'background-color:' + c.Background_Color__c + ';border-color:' + c.Background_Color__c + ';color:' + c.Font_Color__c + ';'));
        }
    }
    
    global override VisualEditor.DataRow getDefaultValue() {
        return myValues.get(0);
    }

    global override VisualEditor.DynamicPickListRows getValues() {
        return myValues;
    }
}
Any help is appreciated!
/Jesper
Best Answer chosen by Jesper Klang.
Jesper Klang.Jesper Klang.
Thanks Swetha for your reply!

I still got the same error after changing the myValues variable to public. However, you did lead me in the right direction :) I found the issue, and it was that I hadn't made the class constructor public. I did change the myValues to public as well as you suggested, because it made it easy to add custom values in the Test Class to test, instead of testing the Custom Metadata Type records from the org. Here's the final code and the Test Class that now works great.

Class
global class brandedSectionColorPicklistValues extends VisualEditor.DynamicPickList {
    global VisualEditor.DynamicPickListRows myValues = new VisualEditor.DynamicPickListRows();

    global brandedSectionColorPicklistValues() {
        for (Brand_Color__mdt c : [SELECT MasterLabel, Background_Color__c, Font_Color__c FROM Brand_Color__mdt ORDER BY MasterLabel ASC]) {
            myValues.addRow(new VisualEditor.DataRow(c.MasterLabel, 'background-color:' + c.Background_Color__c + ';border-color:' + c.Background_Color__c + ';color:' + c.Font_Color__c + ';'));
        }
    }
    
    global override VisualEditor.DataRow getDefaultValue() {
        return myValues.get(0);
    }

    global override VisualEditor.DynamicPickListRows getValues() {
        return myValues;
    }
}

Test Class
@isTest 
public with sharing class brandedSectionColorPicklistValuesTest {
    @isTest
    static void testAllValues() {
        // Creates own values to get consistent test results without relying on Custom Metadata Types existing in org
        VisualEditor.DynamicPickListRows testValues = new VisualEditor.DynamicPickListRows();
        testValues.addRow(new VisualEditor.DataRow('RED', 'background-color:' + '#000' + ';border-color:' + '#000' + ';color:' + '#FFF' + ';'));
        testValues.addRow(new VisualEditor.DataRow('BLACK', 'background-color:' + '#000' + ';border-color:' + '#000' + ';color:' + '#FFF' + ';'));

        brandedSectionColorPicklistValues bSCPV = new brandedSectionColorPicklistValues();
        bSCPV.myValues = testValues; // Replacing the Custom Metadata Type values with the above testValues
        VisualEditor.DynamicPickListRows rows = bSCPV.getValues();
        System.assertEquals(2, rows.size(), 'No Brands were found');
    }
    @isTest
    static void testDefaultValue() {
        // Creates own values to get consistent test results without relying on Custom Metadata Types existing in org
        VisualEditor.DynamicPickListRows testValues = new VisualEditor.DynamicPickListRows();
        testValues.addRow(new VisualEditor.DataRow('RED', 'background-color:' + '#000' + ';border-color:' + '#000' + ';color:' + '#FFF' + ';'));
        testValues.addRow(new VisualEditor.DataRow('BLACK', 'background-color:' + '#000' + ';border-color:' + '#000' + ';color:' + '#FFF' + ';'));

        brandedSectionColorPicklistValues bSCPV = new brandedSectionColorPicklistValues();
        bSCPV.myValues = testValues; // Replacing the Custom Metadata Type values with the above testValues
        VisualEditor.DataRow row = bSCPV.getDefaultValue();
        System.assertEquals('RED', row.getLabel(), 'RED was not the default value');
    }
}

 

All Answers

SwethaSwetha (Salesforce Developers) 
HI Jesper,
Can you set this line to public instead of private to see if that fixes the error?

private VisualEditor.DynamicPickListRows myValues = new VisualEditor.DynamicPickListRows();

Related:https://salesforce.stackexchange.com/questions/270431/how-to-write-a-test-for-a-visualeditor-dynamicpicklist-class
https://salesforce.stackexchange.com/questions/225787/method-is-not-visible-apex-trailhead-unit-testing-challenge

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Jesper Klang.Jesper Klang.
Thanks Swetha for your reply!

I still got the same error after changing the myValues variable to public. However, you did lead me in the right direction :) I found the issue, and it was that I hadn't made the class constructor public. I did change the myValues to public as well as you suggested, because it made it easy to add custom values in the Test Class to test, instead of testing the Custom Metadata Type records from the org. Here's the final code and the Test Class that now works great.

Class
global class brandedSectionColorPicklistValues extends VisualEditor.DynamicPickList {
    global VisualEditor.DynamicPickListRows myValues = new VisualEditor.DynamicPickListRows();

    global brandedSectionColorPicklistValues() {
        for (Brand_Color__mdt c : [SELECT MasterLabel, Background_Color__c, Font_Color__c FROM Brand_Color__mdt ORDER BY MasterLabel ASC]) {
            myValues.addRow(new VisualEditor.DataRow(c.MasterLabel, 'background-color:' + c.Background_Color__c + ';border-color:' + c.Background_Color__c + ';color:' + c.Font_Color__c + ';'));
        }
    }
    
    global override VisualEditor.DataRow getDefaultValue() {
        return myValues.get(0);
    }

    global override VisualEditor.DynamicPickListRows getValues() {
        return myValues;
    }
}

Test Class
@isTest 
public with sharing class brandedSectionColorPicklistValuesTest {
    @isTest
    static void testAllValues() {
        // Creates own values to get consistent test results without relying on Custom Metadata Types existing in org
        VisualEditor.DynamicPickListRows testValues = new VisualEditor.DynamicPickListRows();
        testValues.addRow(new VisualEditor.DataRow('RED', 'background-color:' + '#000' + ';border-color:' + '#000' + ';color:' + '#FFF' + ';'));
        testValues.addRow(new VisualEditor.DataRow('BLACK', 'background-color:' + '#000' + ';border-color:' + '#000' + ';color:' + '#FFF' + ';'));

        brandedSectionColorPicklistValues bSCPV = new brandedSectionColorPicklistValues();
        bSCPV.myValues = testValues; // Replacing the Custom Metadata Type values with the above testValues
        VisualEditor.DynamicPickListRows rows = bSCPV.getValues();
        System.assertEquals(2, rows.size(), 'No Brands were found');
    }
    @isTest
    static void testDefaultValue() {
        // Creates own values to get consistent test results without relying on Custom Metadata Types existing in org
        VisualEditor.DynamicPickListRows testValues = new VisualEditor.DynamicPickListRows();
        testValues.addRow(new VisualEditor.DataRow('RED', 'background-color:' + '#000' + ';border-color:' + '#000' + ';color:' + '#FFF' + ';'));
        testValues.addRow(new VisualEditor.DataRow('BLACK', 'background-color:' + '#000' + ';border-color:' + '#000' + ';color:' + '#FFF' + ';'));

        brandedSectionColorPicklistValues bSCPV = new brandedSectionColorPicklistValues();
        bSCPV.myValues = testValues; // Replacing the Custom Metadata Type values with the above testValues
        VisualEditor.DataRow row = bSCPV.getDefaultValue();
        System.assertEquals('RED', row.getLabel(), 'RED was not the default value');
    }
}

 
This was selected as the best answer