0

I'm completely new to Apex, and self-teaching a lot through examples. I just completed [with this community's help!] a custom VF page with "industry news" twitter feeds. It's a basic page, with each feed as an individual component and a drop down to show/hide the component pageblock.

Now I'm trying to write my first test class, and understand how to do these. Since the controller doesn't call any dynamic data from the SF database [so I can't render an account and verify the details], I presume my test class should simply be testing a selection from the drop down, and making sure the correct pageblock id is rendered.

First, if I'm wrong, please let me know!

Second, I can't quite get my language right to have it test this. There are such limited examples out there for a static picklist, I can't figure out what I'm doing wrong. Suggestions are appreciated!

My page:

<apex:page controller="tweetfeedDropdown">

<apex:pageBlock title="Twitter Feeds">
    Please choose what you would like to see in your feed.<br/><br/>
        <apex:form>
        <apex:actionregion>
        <apex:selectList value="{!selectedValue}" size="1" >
        <Apex:selectOptions value="{!someList}"/>
            <apex:actionSupport event="onchange" action="{!updateData}"  />    
        </apex:selectList> <br/><br/>
        </apex:actionregion>
        </apex:form>

    <apex:pageblocksection  id="TweetFeed_Initial" rendered="{!TweetFeed_Initial}" >
        <c:tweetfeed_1></c:tweetfeed_1>
    </apex:pageblocksection>

    <apex:pageblocksection  id="TweetFeed_2" rendered="{!TweetFeed_2}" >
        <c:tweetfeed_2></c:tweetfeed_2>
    </apex:pageblocksection>

    <apex:pageblocksection  id="TweetFeed_3" rendered="{!TweetFeed_3}" >
        <c:tweetfeed_3></c:tweetfeed_3>
    </apex:pageblocksection>

My controller:

    public class tweetfeedDropdown {
  public Boolean TweetFeed_Initial {get; set;}
  public Boolean TweetFeed_2 {get; set;}
  public Boolean TweetFeed_3 {get; set;}
  public List<Selectoption> someList {get; set;}
  public String selectedValue {get; set;}

  public tweetfeedDropdown() {
    TweetFeed_Initial = true;
    TweetFeed_2 = false;
    TweetFeed_3 = false;
    someList = new List<SelectOption>{new selectoption('TFInitial','Choose a Feed Category'),
                                      new selectoption('TF2','Feed Title 2'),
                                      new Selectoption('TF3','Feed Title 3')};
  }

  public pageReference updateData() {
     if(selectedValue == 'TFInitial') {
          TweetFeed_Initial = true;
          TweetFeed_2 = false;
          TweetFeed_3 = false;
     } else if(selectedValue == 'TF2') {
          TweetFeed_Initial = false;
          TweetFeed_2 = true;
          TweetFeed_3 = false;
     } else if(selectedValue == 'TF3') {
          TweetFeed_Initial = false;
          TweetFeed_2 = false;
          TweetFeed_3 = true;             
     }
     return null;
  } 

My mutilation of a test class:

@isTest
public class tweetfeedDropdownTest {
    static testMethod void tweetfeedDropdownTest(){

    tweetfeedDropdown tfdd = new tweetfeedDropdown();
        List<SelectOption> testoptions = new List<SelectOption>{};
            testoptions=tfdd.getsomeList();
        system.assertEquals(3,testoptions.size());
        string[] testselect = new string[]{};
            testselect.add('TF3');
        tfdd.setsomeList(testselect);
        tfdd.test();
        system.assertEquals(testselect,tfdd.getsomeList());

    }}

FYI...my "example" I followed after reading multiple was from this post, and "JimRae's" answer. Thanks!

  • Thanks Crop1645. That was so simple...yet, I hadn't been able to get it to work before. Thanks! – LindseyG Jul 2 '15 at 16:24
0

To test getters and setters as well as simulate the user selection of a picklist item:

tfdd.selectedValue = 'TFInitial';
tfdd.updateData();
System.assertEquals(true, tfdd.TweetFeed_initial);
System.assertEquals(false, tfdd.TweetFeed_2);
System.assertEquals(false, tfdd.TweetFeed_3);

and repeat for the other picklist values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.