[SalesForce] Controller Extension Test – action method in actionSupport not invoked automatically

Assuming I have two picklists in my VF page, something like this:

<apex:selectList value="{!parentId}" id="cmbParent" size="1" required="true" label="Parent">
  <apex:selectOptions value="{!parents}" />
  <apex:actionSupport event="onchange" action="{!clearChild}" reRender="cmbChild"/>
</apex:selectList>
<apex:selectList value="{!childId}" id="cmdChild" size="1" label="Child">
  <apex:selectOptions value="{!children}" />
</apex:selectList>

In a nutshell, if someone changes the value of the parent picklist, that should call the action method clearChild() and then re-render the child picklist.

I would like to know, in the test class, even if I have called the Test.setCurrentPage, if I programmatically change the parentId, is that not supposed to call the action method automatically?

i.e.

TestCase__c  testcase = new TestCase();
Test.setCurrentPage(Page.MyTestPage);

ApexPages.standardController std = new ApexPages.standardController(testcase);
MyTestCaseExt controllerExt = new MyTestCaseExt(std);

controllerExt.parentId.value = 3;

// after the above line, can I count on the action method clearChild getting called?
// Or do I need to call it explicitly?
//controllerExt.clearChild();
System.assertEquals(null, controllerExt.childId);

I'm asking because it seems to me the action method does not actually get called even though I have specifically set the page reference.

Originally I thought when the parent picklist value changes, it will automatically call the action method to clear the content of the child picklist. If I have to call the action method myself, then obviously my assert will be successful. It'll be like, I set variable a to 3, and then I check if a equals 3, which is kind of pointless, is it not?

Thanks
King

Best Answer

The test environment really only handles Apex code. AFAIK the only value of Test.setCurrentPage is to fake the return value of ApexPages.currentPage() that an Apex controller might rely on. The Visualforce page is not executed to produce HTML and there is no emulation of a browser and its DOM events such as "onchange".

So your Apex test code has to take the place of the Visualforce and directly call the methods and reference the properties. That makes it possible to check that the Apex does what you expect it to to. If your Visualforce is doing relatively little, then that is probably enough. And the Visualforce can be manually tested...

But if you want to automate the testing of how the overall combination of Visualforce (and perhaps jQuery) and the Apex controller work together, then you will have to build UI-level tests too and Selenium is the commonly chosen platform to use for that. But it is quite a lot of work and such tests tend to be fragile unless carefully written, where a small change in a page can break lots of tests.

Related Topic