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?
EDIT
In the few minutes since I posted, I ran this in the developer console:
@isTest
public class UT_Test {
@isTest
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!