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
RaizersRaizers 

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


 
aalbertaalbert
Here is a quick and simple example of writing a test method for this sample controller class. It simply illustrates how you can define the controller like any other apex class instantiation. Secondly, it shows how you can call each getter and/or setter in this sample controller.

With a more full-featured controller, the test methods should execute and verify the controller functionality including the setters, getters, and action methods.

Code:
static testMethod void myTest(){
        
             //Example of instantiation the controller
sampleCon controller = new sampleCon();
//Example of calling a public method in the controller apex class
List<SelectOption> options = controller.getItems();
//Example of calling a setter method
controller.setCountries('US');

String country = controller.getCountries();
//Important to enable assertions to validate expected code behavior
System.assert(country=='US','Error'); }

 

RaizersRaizers

Thanks for the quick reply.  I put the testmethod code provided into a class to test.....am receiving error msgs (lines in red).

Code:
public class TestMeth_SampleCon {

static testMethod void myTest(){
        
             //Example of instantiation the controller             
             sampleCon controller = new sampleCon();
             
             //Example of calling a public method in the controller apex class             
             List<SelectOption> options = controller.getItems();
             
             //Example of calling a setter method             
              controller.setCountries('US');
              
              String country = controller.getCountries();

             //Important to enable assertions to validate expected code behavior              
             System.assert(country=='US','Error');
        }
}

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
MohanaGopalMohanaGopal

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);

JimRaeJimRae
Here is a full example that provides 100% coverage with documentation on how to test a list.

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;
    }
    public static testmethod void sampletest(){
     
     //Create a reference to the VF Page being tested, this is needed if you are passing parameters in the URL
     //or if you are going to have actions on the page that direct to a different page.
     PageReference pageRef = Page.testing;
     Test.setCurrentPage(pageRef);
          
     //Instantiate the controller
     sampleCon controller = new sampleCon();
     
     //To test that the list is being generated is accurate, create a testlist to hold the results of the page list 
     List<SelectOption> testoptions = new List<SelectOption>{};
  
  //set your test list equal to the execution of the test controller
     testoptions=controller.getItems();
     
     //I like to put in some debug statements to see what the objects look like, this is optional, and doesn't count
     //toward your coverage count
     system.debug(testoptions); 
     
     //make sure our test list has 3 items in it, just like the real one did, you could also verify 
     //that the values were the same, if that is a concern
     system.assertequals(3,testoptions.size());
  
  //because the check boxes are in array form, create a test array to select box or boxes for testing     
     String[] testselect = new String[]{};
     
     //add a sample value to the array
     testselect.add('CANADA');
     
     //simulate checking the box by executing the checkbox method with your test value
     controller.setCountries(testselect);
     
     //push the button on the page
     controller.test();
    
     //verify that the result that appeared (the datalist) countains the same values as you selected
     system.assertequals(testselect,controller.getCountries()); 
    }
}

 

RaizersRaizers
Jim.  Thanks for the testing example code and the excellent explanations/documentation.  This has clarified some things for me and I expect other people too.
DetirtusDetirtus

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?

public class MyController {

    List<Opportunity> 
    
    public List<Opportunity> getOpps() {
        return [select name, nextStep from Opportunity limit 10];
    }
}

 Thanks in advance