1

I have the following extension class that I'm using to build a visual force page. It is intended to display a filtered list of Campaign History, rather than the existing related list. It works. That's not the question.

//VF extension, designed to extend the Contact standard controller to display a filtered campaign history list
public class CampaignHistoryExtension {

//this is the Contact ID variable passed by the standard controller
    private ID thisContactID;

//List variable of campaign member records to return to the page as the sorted campaign list
    private transient List<campaignMember> campaignMemberRecords;

//Constructor to extend the standard Contact controller a\@param controller
//@param the standardController object constructed by specifying the standardControler attribute in an apex:page
    public CampaignHistoryExtension(ApexPages.StandardController controller) {
        thisContactID = controller.getId();
        refreshCampaignMemberRecords();
    }

//@return the list of Campaign Member records to present as the campaign history
    public LIST <CampaignMember> getCampaignMemberRecords(){
        return campaignMemberRecords;
    }    

//refresh the list of campaign member records presented as teh contact campaign history by qurying the db using known properties as filter
    private void refreshCampaignMemberRecords(){
        campaignMemberRecords = [
            SELECT ID, CampaignID, Campaign.Name, Campaign.enddate, ContactID, Contact.Name, Status, 
                Campaign.Type, campaign.Display_Campaign_on_Contact__c, LastModifiedDate
            FROM CampaignMember
            WHERE ContactID = :thisContactID
            AND Campaign.Display_Campaign_on_Contact__c=TRUE
            ORDER BY Campaign.Name];
    }
}

The question is, I now am working to create a test class to get coverage. My assumption is that I would need to create a Contact, a Campaign, and a Campaign Member record, and then check if the campaignMemberRecords query returns an appropriate number (1, in this example) of records. I could expand the datasets to have more Contacts and more Campaign Members, but I'm wondering if there's something more I should be testing, or is that all I'l likely need to cover?

Am I on the right track? Am I missing something?

3
  • 1
    You are definitely on the right track. I think you have it. You could take it a bit further and create campaigns and members that would not show up in your query and assert that they aren't included in your query results. Really though, it sounds like you have a solid grasp of what you need to do for a test class. Commented Jan 15, 2015 at 20:18
  • 1
    Yes, those are best practices. This is a fairly simple controller though were you aren't doing any DML so I think you would be all set. Yes, in general though, you should be testing expected behavior (positive), unexpected behavior (negative), and you should always test conditions in bulk as well. Commented Jan 15, 2015 at 20:29
  • Followup question? I'm doing some digging through StackExchange as well as developer.salesforce.com, and some of the articles reference testing the actual page, with something like System.assertEquals('/apex/failure?error=noParam', nextPage) as well as the data the controller is trying to return. Is that also a best practice? Or is that more useful when it's a full page, and not an element on a standard layout?
    – HomerJ
    Commented Jan 15, 2015 at 21:17

1 Answer 1

2

You'll need to setup the test environment, then test the class's behaviour.

  1. Setup the data required by your page and controller.
  2. Set the page you want to test as current, so you'll be able to access it via the api then.
  3. Instanciate the controller as necessary depending on what you declared in your page.
  4. Test the controller as much as you can ! Use assertions, and test positive, negative and bulk behavior as /u/sfdc_ninja stated. Add to this testing various profiles and you're good to go.

Here is a simplified example with:

<apex:page standardController="Account" [...] id="example_page"/>

The test method in your test class:

@isTest(seeAllData=false) static void test_example(){
    // Arrange
    Account test_acc = new Account( name="test" );
    insert test_acc;
    User test_usr = new User( profileId=[..your target audience..]);
    insert test_usr;
    String expected = 'something'; // Replace with what you want
    Test.setCurrentPage( Page.example_page );

    // Act
    system.runAs( test_usr ){
        StandardController ctrl = new StandardController(test_acc);
        String result = ctrl.some_method();
    }

    // Assert
    system.assertEquals( expected, result );
}
1
  • This is great! I'm not having any issue with the data sets (although, adding an account, with related contacts, with related campaign members has cause me some difficulty, passing IDs), but what/how to test a page and the soql is definitely what I was hoping to hear. Thanks to everyone!
    – HomerJ
    Commented Jan 16, 2015 at 15:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .