Sigh. So I am required to cover my Apex code with tests.

I'm testing a piece of code that dynamically builds a tree of controls with a PageBlock as the root object. This code works fine when NOT called from a test.

When this code is called from a test, the error message as per the title is produced. I suspect that any visual component would have this issue, it just happens that the first control I'm dynamically adding is a SelectList.

Component.Apex.SelectList ctl = new Component.Apex.SelectList();
ctl.id = item.name;
ctl.multiselect = false;
ctl.size = 1;
System.debug('ABOUT TO SET LABEL TO \"' + item.label + '\"');
ctl.label = item.label;
System.debug('MANAGED TO SET LABEL OK');

The last line never gets hit, and the data in item.label just looks normal. The log looks like this:

15:25:09.177 (1177886723)|USER_DEBUG|[82]|DEBUG|ABOUT TO SET LABEL TO "Type Of License"
15:25:09.177 (1177893124)|SYSTEM_METHOD_EXIT|[82]|System.debug(ANY)
15:25:09.177 (1177901217)|SYSTEM_METHOD_ENTRY|[83]|FieldProps.__sfdc_label()
15:25:09.177 (1177916661)|SYSTEM_METHOD_EXIT|[83]|FieldProps.__sfdc_label()
15:25:09.178 (1178408276)|EXCEPTION_THROWN|[EXTERNAL]|System.VisualforceException: Invalid value for property label: null
15:25:09.178 (1178560531)|SYSTEM_MODE_EXIT|false
15:25:09.178 (1178650763)|FATAL_ERROR|System.VisualforceException: Invalid value for property label: null

Can this be done?


In the few minutes since I posted, I ran this in the developer console:

public class UT_Test {
    public static void TestSomething() {
        Component.Apex.SelectList ctl = new Component.Apex.SelectList();
        ctl.id = 'myList';
        ctl.multiselect = false;
        ctl.size = 1;
        ctl.label = 'My List';

And sure enough it fails with the 'System.VisualforceException: Invalid value for property label: null'. When the "ctl.label 'My List';" line is commented out, the test succeeds.

So how on earth do I test a large piece of custom APEX code that dynamically creates components?

To package and get my code from my Developer Org into my clients Production Org I HAVE to subject it to unit tests with an AVERAGE of 75% code coverage across code and triggers. I don't mind writing the tests, but come on, at least let me win this one!

2 Answers 2


The label attribute that was introduced Winter12 fails for most form inputs when in test context

    Component.Apex.inputText c1 = new Component.Apex.InputText();
    c1.label = 'test'; // this will fail

but this being salesforce, there are exceptions

    Component.Apex.inputField i1 = new Component.Apex.inputfield();
    i1.label = 'test'; // this works fine

Since creating dynamic components in apex isn't considered idiomatic, I doubt there is an easy way around it.


How important label for your test? If you don't specify label it works. You can remove line "ct1.label = 'My List';" and it will work.

  • 1
    It's important. I've simplified the code to illustrate the point easily. The real class under test produces a graph of dynamically created VisualForce components each of which has its label set. I cannot change that code to not set the label of each component, and it would be stupid to add a flag set only when testing, to conditionally set the label. Thanks for showing interest in my problem, I appreciate it.
    – IanT8
    Aug 26, 2014 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.