You need to sign in to do that
Don't have an account?
Raizers
List TestMethod
Struggling with understanding how to create testmethods so I can package my code to move from development to production. While there is information in the manuals, blogs, and discussion boards, creating testmethods is still confusing. From the discussion boards, I see that I'm not alone. I've searched, but haven't been able to find an example testmethod for Lists.....
Could someone describe:
- How they determine what needs to be tested, and
- How they would go about testing the following sample code (from cookbook)
and provide testmethod code for this sample List code.
Code:
VF Page: <apex:page controller="sampleCon"> <apex:form > <apex:selectCheckboxes value="{!countries}"> <apex:selectOptions value="{!items}"/> </apex:selectCheckboxes><br/> <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/> </apex:form> <apex:outputPanel id="out"> <apex:actionstatus id="status" startText="testing..."> <apex:facet name="stop"> <apex:outputPanel > <p>You have selected:</p> <apex:dataList value="{!countries}" var="c">{!c}</apex:dataList> </apex:outputPanel> </apex:facet> </apex:actionstatus> </apex:outputPanel> </apex:page> CONTROLLER Code: public class sampleCon { String[] countries = new String[]{}; public PageReference test() { return null; } public List<SelectOption> getItems() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('US','US')); options.add(new SelectOption('CANADA','Canada')); options.add(new SelectOption('MEXICO','Mexico')); return options; } public String[] getCountries() { return countries; } public void setCountries(String[] countries) { this.countries = countries; } }
Thanks in advance....
With a more full-featured controller, the test methods should execute and verify the controller functionality including the setters, getters, and action methods.
Thanks for the quick reply. I put the testmethod code provided into a class to test.....am receiving error msgs (lines in red).
controller.setCounties('US'); gets an error msg "Compile Error: Method does not exist or incorrect signature: [sampleCon].setCountries(String)"
String country = countroller.getCounties(); gets an error msg "Compile Error: Illegal assignment from LIST:String to String"
Any ideas on why the error msgs and how to fix ?
Message Edited by Raizers on 12-30-2008 10:07 PM
Hi..
Here Countries method return type is string array.. u have to pass string array to the test method
Try this
String[] country=new String[2];
country[0]='india';
country[1]='pakistan';
controller.setCountries(country);
Hi,
This was helpful, but I'm a total noobie with test methods and therefore was wondering, how to write a test method for the following code?
Thanks in advance