+ Start a Discussion
BaguiarBaguiar 

Change VF page value on a selectlist for a Test Class

Hi there,

 

I have this VF page that runs a report based on the select list criteria. I had the class and test class working fine, until I had to add the option "All" onm the selectlist and , therefore, modify the controller for the new value. Got the controller to work but on my test class, I'm having a problem in testing exactly the ELSE statements. I think because that requires me to test the class also with other values from the selectlist.

 

Any help is appreciated! Here goes the Class, page and test class: (I've made the lines not covered on the class underlined and italic.)

 

public class TaskLeaderNO
{ 
    public PageReference Refreshtsk() {
        return page.Task_LeadershipNO;
    }
    
     public PageReference toexcel() {
        return page.Task_LeadershipXLSNO;
    }
     
  public String positions {get; set;}
  public String rangedate {get; set;}
  public String tasktype {get; set;}
  
  Date candDate;
  Date candDate1;

  public TaskLeaderNO()
  {
     rangedate='30';
     candDate=System.today(); 
     tasktype='All';  
     positions='All';   
  }

   Set<Id> tskNo = new Set<Id>();
 
  private List<Contact> ContactNO;
  public List<Contact> getContactNO()
   {
   candDate1=System.today()-Integer.valueOf(rangedate);
   tskNo = new set<ID>();
   
   IF (tasktype == 'All') {
   for (Task t: [Select WhoID FROM task WHERE OwnerID = :UserInfo.getUserId() and activitydate >= :candDate1])
   tskNo.add(t.WhoID);   
     } else {
 for (Task t: [Select WhoID FROM task WHERE OwnerID = :UserInfo.getUserId() and activitydate >= :candDate1 and type= :tasktype]) tskNo.add(t.WhoID); }   
   
    IF (positions == 'All') {
  
   Contact[] ContactNO = [Select id, AccountId, Account.name, Email, Name, account.billingcity, account.billingstate from Contact 

WHERE ID IN ( SELECT Contact__c FROM Leadership_position_code__c WHERE (Position__c = 'President' OR Position__c = 'Education Director' OR 

Position__c ='Co-President' OR Position__c = 'Senior (or only) Rabbi' OR Position__c = 'Administrator/Exec director') and  (Thru_Date__c = 

null OR 

Thru_Date__c > :system.today()) and (Start_Date__C <= :system.today()) ) and account.Congregational_Rep__c = :UserInfo.getUserId() 

and ID NOT IN :tskNO];
   return ContactNO;
    } else {

 Contact[] ContactNO = [Select id, AccountId, Account.name, Email, Name, account.billingcity, account.billingstate from Contact WHERE ID IN ( SELECT Contact__c FROM Leadership_position_code__c WHERE Position__c = :positions and (Thru_Date__c = null OR Thru_Date__c > :system.today()) and (Start_Date__C <= :system.today()) ) and account.Congregational_Rep__c = :UserInfo.getUserId() and ID NOT IN :tskNO]; return ContactNO;

     }
   }

}

 

Page:

<apex:page controller="TaskLeaderNO" showHeader="false" >
<apex:form >
<apex:pageBlock id="pageBlock" Title="NO Activities with Key Leadership">
<apex:pageMessages ></apex:pageMessages>
<b>Select Position:   </b>
<apex:selectList id="Positions" value="{!positions}" size="1">
<apex:selectoption itemLabel="All" itemValue="All"></apex:selectoption>
<apex:selectoption itemLabel="President" itemValue="President"></apex:selectoption>
<apex:selectoption itemLabel="Co-President" itemValue="Co-President"></apex:selectoption>
<apex:selectoption itemLabel="Senior (or only) Rabbi" itemValue="Senior (or only) Rabbi"></apex:selectoption>
<apex:selectoption itemLabel="Administrator/Exec Director" itemValue="Administrator/Exec Director"></apex:selectoption>
<apex:selectoption itemLabel="Education director" itemValue="Education director"></apex:selectoption>
</apex:selectList>
<b> How many days back? </b>
<apex:selectList id="rangedate" value="{!rangedate}" size="1" >
<apex:selectoption itemLabel="30" itemValue="30"></apex:selectoption>
<apex:selectoption itemLabel="60" itemValue="60"></apex:selectoption>
<apex:selectoption itemLabel="90" itemValue="90"></apex:selectoption>
<apex:selectoption itemLabel="180" itemValue="180"></apex:selectoption>
</apex:selectList>
<b>   </b>
<b> Activity Type </b>
<apex:selectList id="tasktype" value="{!tasktype}" size="1" >
<apex:selectoption itemLabel="All" itemValue="All"></apex:selectoption>
<apex:selectoption itemLabel="Phone Call" itemValue="Phone Call"></apex:selectoption>
<apex:selectoption itemLabel="Email" itemValue="Email"></apex:selectoption>
</apex:selectList>
<b>   </b>
<apex:commandButton action="{!Refreshtsk}" value="Run Report" />
<b>   </b>
<apex:commandButton action="{!toexcel}" value="Export to Excel" />
<apex:pageBlockTable value="{!ContactNO}" var="cont" rendered="{!NOT(ISNULL(ContactNO))}" id="tblContactNo">
<apex:column headerValue="Name" ><apex:outputLink value="/{!cont.id}" target="_parent">{!cont.Name}
</apex:outputLink></apex:column>
<apex:column value="{!cont.Email}"></apex:column>
<apex:column headerValue="Congregation" value="{!cont.account.name}"></apex:column>
<apex:column value="{!cont.account.billingcity}"></apex:column>
<apex:column value="{!cont.account.billingstate}"></apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>

</apex:page>

 And test:

 

@isTest
private  class TestTaskleaderNO
{
   static testMethod void testgetContactNO()
   {
      // insert a test account
      Account acc=new Account(Name='Test123');
      Database.saveresult sr = database.insert(acc);
     
      // insert a test contact
      List<Contact> con=new List<Contact>{new Contact(FirstName='Lester', Lastname='DbigTester', accountID=acc.id, email='123123123@888555.com', Key_Contact__C = True), 
            new Contact(FirstName='Lester2', Lastname='DbigTester2', accountID=acc.id, email='2222222@22225.com', Key_Contact__C = True)};
      insert con;

      // insert some test leadership codes
      List<leadership_position_code__c> LDS=new List<leadership_position_code__c>{new leadership_position_code__c(Contact__C=con[0].id, position_type__C='Officer', Position__C='President', Start_Date__C= system.Today()-1 )};
      insert LDS;

    List<task> tasks = new List<task>{ new task(WhoId=con[0].Id,Subject='HEllo Task',ActivityDate=System.today()-10, activity_type__C = 'Email', Priority = 'Normal', Status = 'In Progress' ),
                                        new task(WhoId=con[1].Id,Subject='HEllo Task2',ActivityDate=System.today()-12, activity_type__C = 'Email', Priority = 'Normal', Status = 'In Progress' )   };
    insert tasks;


      // captures the acc id for the page     
      


      // instantiate the class under test
      TaskLeaderNO tl=new TaskLeaderNO();
     
      List<Contact> ContOK=tl.getContactNO();
      tl.refreshtsk();
      tl.toexcel();

      // change the number below to the number of leadership_position_code__c objects created above
      System.assertEquals(0, ContOK.size());
      
            
   }

 Thanks again!

 

B

 

Edwin VijayEdwin Vijay

To test this behaviour, and if you want to cover this selcet list in your code.

 

<apex:selectList id="tasktype" value="{!tasktype}" size="1" >
<apex:selectoption itemLabel="All" itemValue="All"></apex:selectoption>
<apex:selectoption itemLabel="Phone Call" itemValue="Phone Call"></apex:selectoption>
<apex:selectoption itemLabel="Email" itemValue="Email"></apex:selectoption>
</apex:selectList>

 In your test class, you need to modify the value of the "tasktype" variable, and then call the method which processes the action. The selectlist value variable holds the value of the selected dropdown value in your visualforce page, and hence modifying this value in the test class simulates modifying the drop down in a actual page.

 

Hope it is clear :)

BaguiarBaguiar

Thanks Edwin for the reply!

 

I thought it had to be the change of the varialbel but my problem is exaclty how do i call that change from the test method on the VF page?